Class: ISO3166::Country

Inherits:
Object
  • Object
show all
Extended by:
CountryClassMethods
Includes:
Emoji
Defined in:
lib/countries/country.rb,
lib/countries/mongoid.rb

Direct Known Subclasses

Country

Constant Summary

Constants included from CountryClassMethods

ISO3166::CountryClassMethods::FIND_BY_REGEX, ISO3166::CountryClassMethods::SEARCH_TERM_FILTER_REGEX

Constants included from Emoji

Emoji::CODE_POINTS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CountryClassMethods

[], all, all_names_with_codes, all_translated, codes, find_all_by, method_missing, new, respond_to_missing?, search, translations

Methods included from Emoji

#emoji_flag

Constructor Details

#initialize(country_data) ⇒ Country

Returns a new instance of Country.



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

def initialize(country_data)
  @country_data_or_code = country_data
  reload
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/countries/country.rb', line 5

def data
  @data
end

Class Method Details

.demongoize(alpha2) ⇒ Object



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

def demongoize(alpha2)
  new(alpha2)
end

.evolve(country) ⇒ Object



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

def evolve(country)
  mongoize(country)
end

.mongoize(country) ⇒ Object



8
9
10
11
12
13
14
# 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

Instance Method Details

#<=>(other) ⇒ Object



46
47
48
# File 'lib/countries/country.rb', line 46

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

#==(other) ⇒ Object



34
35
36
# File 'lib/countries/country.rb', line 34

def ==(other)
  other.respond_to?(:alpha2) && other.alpha2 == alpha2
end

#currencyObject



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

def currency
  Money::Currency.find(data['currency_code'])
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/countries/country.rb', line 38

def eql?(other)
  self == other
end

#hashObject



42
43
44
# File 'lib/countries/country.rb', line 42

def hash
  [alpha2, alpha3].hash
end

#in_eea?Boolean

Returns:

  • (Boolean)


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

def in_eea?
  data['eea_member'].nil? ? false : data['eea_member']
end

#in_eu?Boolean

Returns:

  • (Boolean)


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

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

#local_nameObject



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

def local_name
  @local_name ||= local_names.first
end

#local_namesObject

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



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

def local_names
  ISO3166.configuration.locales = (ISO3166.configuration.locales + languages.map(&:to_sym)).uniq
  reload

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

#mongoizeObject



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

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

#reloadObject



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

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_weekObject



54
55
56
# File 'lib/countries/country.rb', line 54

def start_of_week
  data['start_of_week']
end

#subdivision_names_with_codes(locale = 'en') ⇒ Object



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

def subdivision_names_with_codes(locale = 'en')
  subdivisions.map { |k, v| [ v.translations[locale] || v.name, k ] }
end

#subdivisionsObject Also known as: states



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

def subdivisions
  @subdivisions ||= subdivision_data.inject({}) do |hash, (k, v)|
    hash.merge(k => Subdivision.new(v))
  end
end

#subdivisions?Boolean

Returns:

  • (Boolean)


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

def subdivisions?
  !data['subdivisions'].nil? || File.exist?(subdivision_file_path)
end

#to_sObject



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

def to_s
  data['name']
end

#translated_namesObject



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

def translated_names
  data['translations'].values
end

#translation(locale = 'en') ⇒ Object



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

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

#valid?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/countries/country.rb', line 24

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