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, create_subdivisions, 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

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


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

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

#in_eu?Boolean

Returns:

  • (Boolean)


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

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

#local_nameObject



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

def local_name
  @local_name ||= local_names.first
end

#local_namesObject

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



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

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



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

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



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

def start_of_week
  data['start_of_week']
end

#subdivision_names_with_codes(locale = 'en') ⇒ Object



66
67
68
# File 'lib/countries/country.rb', line 66

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

#subdivisionsObject Also known as: states



58
59
60
61
62
63
64
# File 'lib/countries/country.rb', line 58

def subdivisions
  @subdivisions ||= if data['subdivisions']
                      self.class.create_subdivisions(data['subdivisions'])
                    else
                      self.class.subdivisions(alpha2)
                    end
end

#subdivisions?Boolean

Returns:

  • (Boolean)


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

def subdivisions?
  !subdivisions.empty?
end

#to_sObject



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

def to_s
  data['name']
end

#translated_namesObject



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

def translated_names
  data['translations'].values
end

#translation(locale = 'en') ⇒ Object



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

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