Class: Urbans::Province

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProvince

Returns a new instance of Province.



7
8
9
# File 'lib/urbans/foundations/province.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/province.rb', line 2

def __id__
  @__id__
end

#country_idObject

Returns the value of attribute country_id.



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

def country_id
  @country_id
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#namesObject

Returns the value of attribute names.



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

def names
  @names
end

Class Method Details

.all!(_country_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
# File 'lib/urbans/foundations/province.rb', line 11

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

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

  from_file(country_id, locale).each do |_province_id, province_name|
    province_id = _province_id.to_s.downcase.to_sym

    Urbans::PROVINCES[country_id] ||= {}
    province = Urbans::PROVINCES[country_id][province_id]
    if province.nil?
      province = Urbans::Province.new
      province.__id__ = _province_id
      province.id = "#{country_id}.#{province_id}"
      province.names[locale] = province_name
      province.country_id = country_id
      Urbans::PROVINCES[country_id][province_id] = province
    end
    provinces << province
  end
  provinces
end

.from_file(_country_id, _locale) ⇒ Object



84
85
86
87
88
89
# File 'lib/urbans/foundations/province.rb', line 84

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}.provinces.yml")
  from_file[locale]["provinces"][country_id]
end

.get(arg) ⇒ Object

get specific province



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/urbans/foundations/province.rb', line 36

def self.get arg
  _country_id = _province_id = nil
  if arg.is_a?(Hash)
    _country_id, _province_id = arg.fetch(:country), arg[:province]
  elsif arg.is_a?(String)
    _arg = arg.split(".")
    _country_id, _province_id = _arg[0], _arg[1]
  end

  if _province_id.nil?
    return all! _country_id
  end

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

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

  province = Urbans::PROVINCES[country_id][province_id]
  province
end

Instance Method Details

#<=>(another) ⇒ Object

for sorting



80
81
82
# File 'lib/urbans/foundations/province.rb', line 80

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

#citiesObject



75
76
77
# File 'lib/urbans/foundations/province.rb', line 75

def cities
  Urbans.city.get country: country_id, province: __id__
end

#countryObject



71
72
73
# File 'lib/urbans/foundations/province.rb', line 71

def country
  Urbans.country.get country_id
end

#name(_locale = Urbans.locale) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/urbans/foundations/province.rb', line 60

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

  if names[locale].nil?
    province_local_name = self.class.from_file(country_id, locale)[__id__]
    names[locale] = province_local_name
  end

  names[locale]
end