class ISO3166::Country
def ==(other)
def ==(other) other == data end
def [](query)
def [](query) search(query) end
def all(&blk)
def all(&blk) blk ||= proc { |country, data| [data['name'], country] } Data.map &blk end
def all_translated(locale = 'en')
def all_translated(locale = 'en') translations(locale).values end
def currency
def currency ISO4217::Currency.from_code(@data['currency']) end
def currency_code
def currency_code @data['currency'] end
def demongoize(alpha2)
def demongoize(alpha2) new(alpha2) end
def evolve(country)
def evolve(country) mongoize(country) end
def find_all_by(attribute, val)
def find_all_by(attribute, val) attributes, value = parse_attributes(attribute, val) Data.select do |_, v| attributes.map do |attr| Array(v[attr]).any? { |n| value === n.to_s.downcase } end.include?(true) end end
def find_by(attribute, value, obj = nil)
def find_by(attribute, value, obj = nil) find_all_by(attribute.downcase, value).map do |country| obj.nil? ? country : new(country.last) end end
def in_eu?
def in_eu? @data['eu_member'].nil? ? false : @data['eu_member'] end
def initialize(country_data)
def initialize(country_data) @data = country_data.is_a?(Hash) ? country_data : Data[country_data.to_s.upcase] end
def method_missing(*m)
def method_missing(*m) regex = m.first.to_s.match(/^find_(all_)?(country_|countries_)?by_(.+)/) super unless regex countries = find_by(Regexp.last_match[3], m[1], Regexp.last_match[2]) Regexp.last_match[1] ? countries : countries.last end
def mongoize
def mongoize ISO3166::Country.mongoize(self) end
def mongoize(country)
def mongoize(country) if country.is_a?(self) && !country.data.nil? country.alpha2 elsif send(:valid_alpha2?, country) new(country).alpha2 else nil end end
def new(country_data)
def new(country_data) if country_data.is_a?(Hash) || Data.keys.include?(country_data.to_s.upcase) super end end
def parse_attributes(attribute, val)
def parse_attributes(attribute, val) fail "Invalid attribute name '#{attribute}'" unless AttrReaders.include?(attribute.to_sym) attributes = Array(attribute.to_s) if attributes == ['name'] attributes << 'names' # TODO: Revisit when better data from i18n_data # attributes << 'translated_names' end val = (val.is_a?(Regexp) ? Regexp.new(val.source, 'i') : val.to_s.downcase) [attributes, val] end
def search(query)
def search(query) country = new(query.to_s.upcase) (country && country.valid?) ? country : nil end
def subdivisions
def subdivisions @subdivisions ||= subdivisions? ? YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml")) : {} end
def subdivisions?
def subdivisions? File.exist?(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml")) end
def to_s
def to_s @data['name'] end
def translation(locale = 'en')
def translation(locale = 'en') @data['translations'][locale.downcase] end
def translations(locale = 'en')
def translations(locale = 'en') I18nData.countries(locale.upcase) end
def valid?
def valid? !(@data.nil? || @data.empty?) end
def valid_alpha2?(country)
def valid_alpha2?(country) country.is_a?(String) && !ISO3166::Country.new(country).nil? end