Class: City
- Inherits:
-
Object
- Object
- City
- Defined in:
- lib/cities/city.rb
Class Method Summary collapse
- .cities_in_country(code) ⇒ Object
- .cities_in_country?(code) ⇒ Boolean
- .path_for_country(code) ⇒ Object
Instance Method Summary collapse
-
#initialize(data) ⇒ City
constructor
A new instance of City.
- #latitude ⇒ Object
- #latlong ⇒ Object
- #latlong? ⇒ Boolean
- #longitude ⇒ Object
- #name ⇒ Object
- #population ⇒ Object
- #region ⇒ Object
Constructor Details
#initialize(data) ⇒ City
Returns a new instance of City.
3 4 5 |
# File 'lib/cities/city.rb', line 3 def initialize(data) @data = data end |
Class Method Details
.cities_in_country(code) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/cities/city.rb', line 48 def cities_in_country(code) if self.cities_in_country?(code) hash, cities = YAML.load_file(self.path_for_country(code)), {} hash.each{ |key, data| cities[key] = City.new(data) } cities else {} end end |
.cities_in_country?(code) ⇒ Boolean
44 45 46 |
# File 'lib/cities/city.rb', line 44 def cities_in_country?(code) File.exist?(self.path_for_country(code)) end |
.path_for_country(code) ⇒ Object
40 41 42 |
# File 'lib/cities/city.rb', line 40 def path_for_country(code) File.join(File.dirname(__FILE__), '..', 'data', 'cities', "#{code}.yaml") end |
Instance Method Details
#latitude ⇒ Object
11 12 13 14 |
# File 'lib/cities/city.rb', line 11 def latitude return nil if @data["latitude"].nil? @data["latitude"].to_f end |
#latlong ⇒ Object
25 26 27 |
# File 'lib/cities/city.rb', line 25 def latlong latlong? ? [latitude, longitude] : nil end |
#latlong? ⇒ Boolean
21 22 23 |
# File 'lib/cities/city.rb', line 21 def latlong? latitude && longitude end |
#longitude ⇒ Object
16 17 18 19 |
# File 'lib/cities/city.rb', line 16 def longitude return nil if @data["longitude"].nil? @data["longitude"].to_f end |
#name ⇒ Object
7 8 9 |
# File 'lib/cities/city.rb', line 7 def name @data["accentcity"] end |
#population ⇒ Object
29 30 31 32 |
# File 'lib/cities/city.rb', line 29 def population return nil if @data["population"].nil? @data["population"].to_i end |
#region ⇒ Object
34 35 36 |
# File 'lib/cities/city.rb', line 34 def region @data["region"] end |