Class: Urbans::Country

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCountry

Returns a new instance of Country.



11
12
13
# File 'lib/urbans/foundations/country.rb', line 11

def initialize
  self.names = {}
end

Instance Attribute Details

#__id__Object

raw id from file, useful only for things to do that is related with file



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

def __id__
  @__id__
end

#idObject

what the user need to know about the id



6
7
8
# File 'lib/urbans/foundations/country.rb', line 6

def id
  @id
end

#namesObject

by locale => id: Indonesia, en: Indonesia



9
10
11
# File 'lib/urbans/foundations/country.rb', line 9

def names
  @names
end

Class Method Details

.all(locale = Urbans.locale) ⇒ Object

return all loaded countries



16
17
18
# File 'lib/urbans/foundations/country.rb', line 16

def self.all locale=Urbans.locale
  return Urbans::COUNTRIES.values
end

.all!(locale = Urbans.locale) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/urbans/foundations/country.rb', line 20

def self.all! locale=Urbans.locale
  countries = []
  from_file(locale).each do |country_id, country_data|
    country = Urbans::COUNTRIES[country_id.to_s.downcase.to_sym]
    if country.nil?
      country = Urbans::Country.new
      country.__id__ = country_id
      country.id = country_id.to_s.downcase
      country.names[locale] = country_data["name"]
      Urbans::COUNTRIES[country.id.to_sym] = country
    end
    countries << country
  end
  countries
end

.from_file(locale) ⇒ Object



85
86
87
88
# File 'lib/urbans/foundations/country.rb', line 85

def self.from_file locale
  from_file = YAML.load_file(Urbans::URBAN_PATH + "Urbans/data/#{locale}.countries.yml")
  from_file[locale.to_s]["countries"]
end

.get(country_id) ⇒ Object

return instance of a country



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

def self.get country_id
  if country_id.is_a?(Hash)
    country_id = country_id[:country]
  end

  country = Urbans::COUNTRIES[country_id.to_sym]
  if country.nil?
    locale = Urbans.locale
    country_from_file = from_file(locale)[country_id.to_s.upcase]

    # create a new country instance
    country = Urbans::Country.new
    # ID of a country in file is always in uppercase
    country.__id__ = country_id.to_s.upcase
    country.id = country_id.to_s.downcase
    country.names[locale.to_sym] = country_from_file["name"]

    # keep in hash
    Urbans::COUNTRIES[country_id.to_sym] = country
  else
    return country
  end
end

Instance Method Details

#<=>(another) ⇒ Object



81
82
83
# File 'lib/urbans/foundations/country.rb', line 81

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

#cities_by_provinceObject



65
66
67
# File 'lib/urbans/foundations/country.rb', line 65

def cities_by_province

end

#name(_locale = Urbans.locale) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/urbans/foundations/country.rb', line 69

def name _locale=Urbans.locale
  locale = _locale.is_a?(Symbol) ? _locale : _locale.to_s.downcase.to_sym

  if names[locale].nil?
    # try to load
    local_name = self.class.from_file(locale)[__id__]["name"]
    names[locale] = local_name
  end

  names[locale]
end

#provincesObject



61
62
63
# File 'lib/urbans/foundations/country.rb', line 61

def provinces
  Urbans.province.get country: id
end