Class: City

Inherits:
Object
  • Object
show all
Defined in:
lib/cities/city.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:

  • (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

#latitudeObject



11
12
13
14
# File 'lib/cities/city.rb', line 11

def latitude
  return nil if @data["latitude"].nil?
  @data["latitude"].to_f
end

#latlongObject



25
26
27
# File 'lib/cities/city.rb', line 25

def latlong
  latlong? ? [latitude, longitude] : nil
end

#latlong?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/cities/city.rb', line 21

def latlong?
  latitude && longitude
end

#longitudeObject



16
17
18
19
# File 'lib/cities/city.rb', line 16

def longitude
  return nil if @data["longitude"].nil?
  @data["longitude"].to_f
end

#nameObject



7
8
9
# File 'lib/cities/city.rb', line 7

def name
  @data["accentcity"]
end

#populationObject



29
30
31
32
# File 'lib/cities/city.rb', line 29

def population
  return nil if @data["population"].nil?
  @data["population"].to_i
end

#regionObject



34
35
36
# File 'lib/cities/city.rb', line 34

def region
  @data["region"]
end