class ISO3166::Country

Attributes

data[R]

Public Class Methods

demongoize(alpha2) click to toggle source
# File lib/countries/mongoid.rb, line 16
def demongoize(alpha2)
  new(alpha2)
end
evolve(country) click to toggle source
# File lib/countries/mongoid.rb, line 20
def evolve(country)
  mongoize(country)
end
mongoize(country) click to toggle source
# File lib/countries/mongoid.rb, line 8
def mongoize(country)
  if country.is_a?(self) && !country.data.nil?
    country.alpha2
  elsif send(:valid_alpha2?, country)
    new(country).alpha2
  end
end
new(country_data) click to toggle source
# File lib/countries/country.rb, line 19
def initialize(country_data)
  @country_data_or_code = country_data
  reload
end

Private Class Methods

valid_alpha2?(country) click to toggle source
# File lib/countries/mongoid.rb, line 26
def valid_alpha2?(country)
  country.is_a?(String) && !ISO3166::Country.new(country).nil?
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/countries/country.rb, line 47
def <=>(other)
  to_s <=> other.to_s
end
==(other) click to toggle source
# File lib/countries/country.rb, line 35
def ==(other)
  other.respond_to?(:alpha2) && other.alpha2 == alpha2
end
eql?(other) click to toggle source
# File lib/countries/country.rb, line 39
def eql?(other)
  self == other
end
hash() click to toggle source
# File lib/countries/country.rb, line 43
def hash
  [alpha2, alpha3].hash
end
in_eea?() click to toggle source
# File lib/countries/country.rb, line 77
def in_eea?
  data['eea_member'].nil? ? false : data['eea_member']
end
in_esm?() click to toggle source
# File lib/countries/country.rb, line 81
def in_esm?
  data['esm_member'].nil? ? in_eea? : data['esm_member']
end
in_eu?() click to toggle source
# File lib/countries/country.rb, line 73
def in_eu?
  data['eu_member'].nil? ? false : data['eu_member']
end
local_name() click to toggle source
# File lib/countries/country.rb, line 105
def local_name
  @local_name ||= local_names.first
end
local_names() click to toggle source

TODO: Looping through locale langs could be be very slow across multiple countries

# File lib/countries/country.rb, line 98
def local_names
  ISO3166.configuration.locales = (ISO3166.configuration.locales + languages.map(&:to_sym)).uniq
  reload

  @local_names ||= languages.map { |language| translations[language] }
end
mongoize() click to toggle source
# File lib/countries/mongoid.rb, line 3
def mongoize
  ISO3166::Country.mongoize(self)
end
reload() click to toggle source
# File lib/countries/country.rb, line 109
def reload
  @data = if @country_data_or_code.is_a?(Hash)
            @country_data_or_code
          else
            ISO3166::Data.new(@country_data_or_code).call
          end
end
start_of_week() click to toggle source
# File lib/countries/country.rb, line 51
def start_of_week
  data['start_of_week']
end
states()
Alias for: subdivisions
subdivision_names_with_codes(locale = 'en') click to toggle source
# File lib/countries/country.rb, line 67
def subdivision_names_with_codes(locale = 'en')
  subdivisions.map { |k, v| [v.translations[locale] || v.name, k] }
end
subdivisions() click to toggle source
# File lib/countries/country.rb, line 59
def subdivisions
  @subdivisions ||= if data['subdivisions']
                      self.class.create_subdivisions(data['subdivisions'])
                    else
                      self.class.subdivisions(alpha2)
                    end
end
Also aliased as: states
subdivisions?() click to toggle source
# File lib/countries/country.rb, line 55
def subdivisions?
  !subdivisions.empty?
end
to_s() click to toggle source
# File lib/countries/country.rb, line 85
def to_s
  data['name']
end
translated_names() click to toggle source
# File lib/countries/country.rb, line 89
def translated_names
  data['translations'].values
end
translation(locale = 'en') click to toggle source
# File lib/countries/country.rb, line 93
def translation(locale = 'en')
  data['translations'][locale.to_s.downcase]
end
valid?() click to toggle source
# File lib/countries/country.rb, line 24
def valid?
  !(data.nil? || data.empty?)
end