Class: ZipGeoJp::Record
- Inherits:
-
Object
- Object
- ZipGeoJp::Record
- Defined in:
- lib/zip_geo_jp/record.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#latitude ⇒ Object
readonly
Returns the value of attribute latitude.
-
#longitude ⇒ Object
readonly
Returns the value of attribute longitude.
-
#prefecture ⇒ Object
readonly
Returns the value of attribute prefecture.
-
#zip_code ⇒ Object
readonly
Returns the value of attribute zip_code.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(values) ⇒ Record
constructor
A new instance of Record.
Constructor Details
#initialize(values) ⇒ Record
Returns a new instance of Record.
7 8 9 10 |
# File 'lib/zip_geo_jp/record.rb', line 7 def initialize(values) @zip_code = values.shift.gsub(/^(\d{3})(\d{4})$/, '\1-\2') @prefecture, @city, @block, @latitude, @longitude = values end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
5 6 7 |
# File 'lib/zip_geo_jp/record.rb', line 5 def block @block end |
#city ⇒ Object (readonly)
Returns the value of attribute city.
5 6 7 |
# File 'lib/zip_geo_jp/record.rb', line 5 def city @city end |
#latitude ⇒ Object (readonly)
Returns the value of attribute latitude.
5 6 7 |
# File 'lib/zip_geo_jp/record.rb', line 5 def latitude @latitude end |
#longitude ⇒ Object (readonly)
Returns the value of attribute longitude.
5 6 7 |
# File 'lib/zip_geo_jp/record.rb', line 5 def longitude @longitude end |
#prefecture ⇒ Object (readonly)
Returns the value of attribute prefecture.
5 6 7 |
# File 'lib/zip_geo_jp/record.rb', line 5 def prefecture @prefecture end |
#zip_code ⇒ Object (readonly)
Returns the value of attribute zip_code.
5 6 7 |
# File 'lib/zip_geo_jp/record.rb', line 5 def zip_code @zip_code end |
Class Method Details
.[](key) ⇒ Object
13 14 15 16 |
# File 'lib/zip_geo_jp/record.rb', line 13 def [] (key) row = db.execute('SELECT * FROM zip_codes WHERE zip_code = ? LIMIT 1', key).first row ? from_row(row) : nil end |
.[]=(key, values) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/zip_geo_jp/record.rb', line 18 def []= (key, values) values[:zip_code] = key db.execute( 'REPLACE INTO zip_codes(zip_code, prefecture, city, block, latitude, longitude) VALUES(:zip_code, :prefecture, :city, :block, :latitude, :longitude)', values ) values end |
.from_row(row) ⇒ Object
31 32 33 |
# File 'lib/zip_geo_jp/record.rb', line 31 def from_row(row) new(row.values) end |
.reset ⇒ Object
27 28 29 |
# File 'lib/zip_geo_jp/record.rb', line 27 def reset File.delete(file) if File.exists?(file) end |