Class: ISO3166::Country

Inherits:
Object
  • Object
show all
Defined in:
lib/countries/country.rb,
lib/countries/mongoid.rb

Direct Known Subclasses

Country

Constant Summary collapse

Codes =
YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'countries.yaml'))
Translations =
YAML.load_file(File.join(File.dirname(__FILE__), '..', 'cache', 'translations.yaml'))
Data =
{}
Names =
I18nData.countries.values.sort_by { |d| d[0] }
AttrReaders =
[
  :number,
  :alpha2,
  :alpha3,
  :name,
  :names,
  :latitude,
  :longitude,
  :continent,
  :region,
  :subregion,
  :world_region,
  :country_code,
  :national_destination_code_lengths,
  :national_number_lengths,
  :international_prefix,
  :national_prefix,
  :address_format,
  :translations,
  :translated_names,
  :ioc,
  :gec,
  :un_locode,
  :languages,
  :nationality,
  :dissolved_on,
  :eu_member,
  :alt_currency,
  :vat_rates,
  :postal_code,
  :min_longitude,
  :min_latitude,
  :max_longitude,
  :max_latitude,
  :latitude_dec,
  :longitude_dec
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(country_data) ⇒ Country

Returns a new instance of Country.



59
60
61
# File 'lib/countries/country.rb', line 59

def initialize(country_data)
  @data = country_data.is_a?(Hash) ? country_data : Data[country_data.to_s.upcase]
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



57
58
59
# File 'lib/countries/country.rb', line 57

def data
  @data
end

Class Method Details

.[](query) ⇒ Object



134
135
136
# File 'lib/countries/country.rb', line 134

def [](query)
  search(query)
end

.all(&blk) ⇒ Object Also known as: countries



114
115
116
117
# File 'lib/countries/country.rb', line 114

def all(&blk)
  blk ||= proc { |country, data| [data['name'], country] }
  Data.map(&blk)
end

.all_translated(locale = 'en') ⇒ Object



121
122
123
# File 'lib/countries/country.rb', line 121

def all_translated(locale = 'en')
  translations(locale).values
end

.demongoize(alpha2) ⇒ Object



19
20
21
# File 'lib/countries/mongoid.rb', line 19

def demongoize(alpha2)
  new(alpha2)
end

.evolve(country) ⇒ Object



23
24
25
# File 'lib/countries/mongoid.rb', line 23

def evolve(country)
  mongoize(country)
end

.find_all_by(attribute, val) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/countries/country.rb', line 146

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

.method_missing(*m) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/countries/country.rb', line 138

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

.mongoize(country) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/countries/mongoid.rb', line 9

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

.new(country_data) ⇒ Object



108
109
110
111
112
# File 'lib/countries/country.rb', line 108

def new(country_data)
  if country_data.is_a?(Hash) || Data.keys.include?(country_data.to_s.upcase)
    super
  end
end

.search(query) ⇒ Object



129
130
131
132
# File 'lib/countries/country.rb', line 129

def search(query)
  country = new(query.to_s.upcase)
  (country && country.valid?) ? country : nil
end

.translations(locale = 'en') ⇒ Object



125
126
127
# File 'lib/countries/country.rb', line 125

def translations(locale = 'en')
  I18nData.countries(locale.upcase)
end

Instance Method Details

#==(other) ⇒ Object



71
72
73
# File 'lib/countries/country.rb', line 71

def ==(other)
  other == data
end

#currencyObject



75
76
77
# File 'lib/countries/country.rb', line 75

def currency
  ISO4217::Currency.from_code(@data['currency'])
end

#currency_codeObject



79
80
81
# File 'lib/countries/country.rb', line 79

def currency_code
  @data['currency']
end

#in_eu?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/countries/country.rb', line 93

def in_eu?
  @data['eu_member'].nil? ? false : @data['eu_member']
end

#mongoizeObject



4
5
6
# File 'lib/countries/mongoid.rb', line 4

def mongoize
  ISO3166::Country.mongoize(self)
end

#subdivisionsObject Also known as: states



83
84
85
# File 'lib/countries/country.rb', line 83

def subdivisions
  @subdivisions ||= subdivisions? ? YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml")) : {}
end

#subdivisions?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/countries/country.rb', line 89

def subdivisions?
  File.exist?(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml"))
end

#to_sObject



97
98
99
# File 'lib/countries/country.rb', line 97

def to_s
  @data['name']
end

#translation(locale = 'en') ⇒ Object



101
102
103
# File 'lib/countries/country.rb', line 101

def translation(locale = 'en')
  @data['translations'][locale.downcase]
end

#valid?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/countries/country.rb', line 63

def valid?
  !(@data.nil? || @data.empty?)
end