Class: Urbans::City

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCity

Returns a new instance of City.



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

def initialize
  self.names = {}
end

Instance Attribute Details

#__id__Object

Returns the value of attribute __id__.



2
3
4
# File 'lib/urbans/foundations/city.rb', line 2

def __id__
  @__id__
end

#country_idObject

Returns the value of attribute country_id.



5
6
7
# File 'lib/urbans/foundations/city.rb', line 5

def country_id
  @country_id
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/urbans/foundations/city.rb', line 3

def id
  @id
end

#namesObject

Returns the value of attribute names.



4
5
6
# File 'lib/urbans/foundations/city.rb', line 4

def names
  @names
end

#province_idObject

Returns the value of attribute province_id.



5
6
7
# File 'lib/urbans/foundations/city.rb', line 5

def province_id
  @province_id
end

Class Method Details

.all!(_country_id, _province_id, _locale = Urbans.locale) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/urbans/foundations/city.rb', line 11

def self.all! _country_id, _province_id, _locale=Urbans.locale
  cities = []

  country_id = _country_id.to_s.downcase.to_sym
  province_id = _province_id.to_s.downcase.to_sym
  locale = _locale.to_s.downcase.to_sym

  Urbans::CITIES[country_id] ||= {}

  from_file(country_id, locale).each do |_current_province_id, province_cities|
    current_province_id = _current_province_id.to_s.downcase.to_sym
    Urbans::CITIES[country_id][current_province_id] ||= {}

    province_cities.each do |_city_id, city_name|
      city_id = _city_id.to_s.downcase.to_sym

      city = Urbans::CITIES[country_id][current_province_id][city_id]
      unless city
        city = Urbans::City.new
        city.__id__ = _city_id
        city.id = "#{country_id}.#{current_province_id}.#{city_id}"
        city.country_id = country_id
        city.province_id = current_province_id
        Urbans::CITIES[country_id][current_province_id][city_id] = city
      end
      city.names[locale] = city_name

      cities << city if current_province_id == province_id
    end
  end
  cities
end

.from_file(_country_id, _locale) ⇒ Object



95
96
97
98
99
100
# File 'lib/urbans/foundations/city.rb', line 95

def self.from_file _country_id, _locale
  locale = _locale.to_s.downcase
  country_id = _country_id.to_s.downcase
  from_file = YAML.load_file(Urbans::URBAN_PATH + "urbans/data/#{locale}.#{country_id}.cities.yml")
  from_file[locale]["cities"][country_id]
end

.get(arg) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/urbans/foundations/city.rb', line 44

def self.get arg
  _country_id = _province_id = _city_id = nil

  if arg.is_a?(Hash)
    _country_id, _province_id, _city_id = arg.fetch(:country), arg.fetch(:province), arg[:city]
  elsif arg.is_a?(String)
    _arg = arg.split(".")
    _country_id, _province_id, _city_id = _arg[0], _arg[1], _arg[2]
  end

  if _city_id.nil?
    return all! _country_id, _province_id
  end

  country_id = _country_id.to_s.downcase.to_sym
  province_id = _province_id.to_s.downcase.to_sym
  city_id = _city_id.to_s.downcase.to_sym

  if Urbans::CITIES[country_id].nil? || Urbans::CITIES[country_id][province_id].nil? ||
      Urbans::CITIES[country_id][province_id][city_id].nil?
    all! country_id, province_id, Urbans.locale
  end

  city = Urbans::CITIES[country_id][province_id][city_id]
  city
end

Instance Method Details

#<=>(another) ⇒ Object



91
92
93
# File 'lib/urbans/foundations/city.rb', line 91

def <=> another
  self.name <=> another.name
end

#countryObject



83
84
85
# File 'lib/urbans/foundations/city.rb', line 83

def country
  Urbans.country.get country_id
end

#name(_locale = Urbans.locale) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/urbans/foundations/city.rb', line 71

def name _locale=Urbans.locale
  locale = _locale.to_s.downcase.to_sym

  if names[locale].nil?
    cities_by_province = self.class.from_file(country_id, locale)[province_id.to_s.downcase]
    city_local_name = cities_by_province[__id__]
    names[locale] = city_local_name
  end

  names[locale]
end

#provinceObject



87
88
89
# File 'lib/urbans/foundations/city.rb', line 87

def province
  Urbans.province.get country: country_id, province: province_id
end