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

Setup =
ISO3166::Setup.new
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.



52
53
54
# File 'lib/countries/country.rb', line 52

def initialize(country_data)
  @data = country_data.is_a?(Hash) ? country_data : ISO3166::Data.new(country_data).call
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



50
51
52
# File 'lib/countries/country.rb', line 50

def data
  @data
end

Class Method Details

.[](query) ⇒ Object



145
146
147
# File 'lib/countries/country.rb', line 145

def [](query)
  search(query)
end

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



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

def all(&blk)
  blk ||= proc { |_alpha2, d| ISO3166::Country.new(d) }
  Setup.data.map(&blk)
end

.all_names_with_codes(locale = 'en') ⇒ Object



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

def all_names_with_codes(locale = 'en')
  ISO3166::Country.all.map do |c|
    [(c.translation(locale) || c.name).html_safe, c.alpha2]
  end.sort_by { |d| d[0] }
end

.all_translated(locale = 'en') ⇒ Object



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

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

.codesObject



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

def codes
  Setup.codes
end

.demongoize(alpha2) ⇒ Object



17
18
19
# File 'lib/countries/mongoid.rb', line 17

def demongoize(alpha2)
  new(alpha2)
end

.evolve(country) ⇒ Object



21
22
23
# File 'lib/countries/mongoid.rb', line 21

def evolve(country)
  mongoize(country)
end

.find_all_by(attribute, val) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/countries/country.rb', line 157

def find_all_by(attribute, val)
  attributes, value = parse_attributes(attribute, val)

  Setup.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



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

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
# 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
  end
end

.new(country_data) ⇒ Object



111
112
113
# File 'lib/countries/country.rb', line 111

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

.search(query) ⇒ Object



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

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

.translations(locale = 'en') ⇒ Object



136
137
138
# File 'lib/countries/country.rb', line 136

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

Instance Method Details

#<=>(other) ⇒ Object



68
69
70
# File 'lib/countries/country.rb', line 68

def <=>(other)
  to_s <=> other.to_s
end

#==(other) ⇒ Object



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

def ==(other)
  other == data
end

#currencyObject



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

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

#currency_codeObject



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

def currency_code
  @data['currency']
end

#in_eu?Boolean

Returns:

  • (Boolean)


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

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

#local_nameObject



106
107
108
# File 'lib/countries/country.rb', line 106

def local_name
  @local_name ||= local_names.first
end

#local_namesObject



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

def local_names
  @local_names ||= languages.map { |language| translations[language] }
end

#mongoizeObject



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

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

#subdivisionsObject Also known as: states



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

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

#subdivisions?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/countries/country.rb', line 86

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

#to_sObject



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

def to_s
  @data['name']
end

#translation(locale = 'en') ⇒ Object



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

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

#valid?Boolean

Returns:

  • (Boolean)


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

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