class Urbans::Country

Attributes

__id__[RW]

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

id[RW]

what the user need to know about the id

names[RW]

by locale => id: {id: Indonesia, en: Indonesia}

Public Class Methods

all(locale=Urbans.locale) click to toggle source

return all loaded countries

# File lib/urbans/foundations/country.rb, line 16
def self.all locale=Urbans.locale
  return Urbans::COUNTRIES.values
end
all!(locale=Urbans.locale) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

return instance of a country

# 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
new() click to toggle source
# File lib/urbans/foundations/country.rb, line 11
def initialize
  self.names = {}
end

Public Instance Methods

<=>(another) click to toggle source
# File lib/urbans/foundations/country.rb, line 81
def <=> another
  self.name <=> another.name
end
cities_by_province() click to toggle source
# File lib/urbans/foundations/country.rb, line 65
def cities_by_province

end
name(_locale=Urbans.locale) click to toggle source
# 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
provinces() click to toggle source
# File lib/urbans/foundations/country.rb, line 61
def provinces
  Urbans.province.get country: id
end