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



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

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

#==(other) ⇒ Object



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  self == other
end

#hashObject



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

def hash
  [alpha2, alpha3].hash
end

#in_eea?Boolean

Returns:

  • (Boolean)


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

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

#in_eu?Boolean

Returns:

  • (Boolean)


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

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

#local_nameObject



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

def local_name
  @local_name ||= local_names.first
end

#local_namesObject

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



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

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



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

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



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

def start_of_week
  data['start_of_week']
end

#subdivision_names_with_codes(locale = 'en') ⇒ Object



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

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

#subdivisionsObject Also known as: states



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

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

#subdivisions?Boolean

Returns:

  • (Boolean)


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

def subdivisions?
  !subdivisions.empty?
end

#to_sObject



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

def to_s
  data['name']
end

#translated_namesObject



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

def translated_names
  data['translations'].values
end

#translation(locale = 'en') ⇒ Object



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

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