Class: ISO3166::Country

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

Direct Known Subclasses

Country

Constant Summary

Constants included from CountryFinderMethods

ISO3166::CountryFinderMethods::FIND_BY_REGEX, ISO3166::CountryFinderMethods::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, new, pluck, translations

Methods included from CountryFinderMethods

[], find_all_by, method_missing, respond_to_missing?, search

Methods included from TimezoneExtensions

#timezones

Methods included from Emoji

#emoji_flag

Constructor Details

#initialize(country_data) ⇒ Country

Returns a new instance of Country.



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

def initialize(country_data)
  @country_data_or_code = country_data
  reload
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/countries/country.rb', line 8

def data
  @data
end

Class Method Details

.demongoize(alpha2) ⇒ Object

Get the object as it was stored with Mongoid, and instantiate an ISO3166::Country.



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

def demongoize(alpha2)
  new(alpha2)
end

.evolve(country) ⇒ Object

Convert an ISO3166::Country to the data that is stored by Mongoid.



25
26
27
# File 'lib/countries/mongoid.rb', line 25

def evolve(country)
  mongoize(country)
end

.mongoize(country) ⇒ Object

Convert an ISO3166::Country to the data that is stored by Mongoid.



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

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



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

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

#==(other) ⇒ Object



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

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

#common_nameString

Returns the “common name” of this Country in English.

Returns:

  • (String)

    the “common name” of this Country in English.



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

def common_name
  ISO3166.configuration.locales = ISO3166.configuration.locales.append(:en).uniq
  translation('en')
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  self == other
end

#hashObject



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

def hash
  [alpha2, alpha3].hash
end

#humanized_subdivision_typesArray<String>

Returns the list of humanized subdivision types for this country. Uses ActiveSupport’s ‘#humanize` if available.

Returns:

  • (Array<String>)

    the list of humanized subdivision types for this country. Uses ActiveSupport’s ‘#humanize` if available



79
80
81
82
83
84
85
# File 'lib/countries/country.rb', line 79

def humanized_subdivision_types
  if String.instance_methods.include?(:humanize)
    subdivisions.map{|k,v| v['type'].humanize}.uniq
  else
    subdivisions.map{|k,v| v['type'][0].upcase + v['type'].tr('_', ' ')[1..-1]}.uniq
  end
end

#in_eea?Boolean

true if this country is a member of the European Economic Area.

Returns:

  • (Boolean)


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

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

#in_esm?Boolean

true if this country is a member of the European Single Market.

Returns:

  • (Boolean)


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

def in_esm?
  data['esm_member'].nil? ? in_eea? : data['esm_member']
end

#in_eu?Boolean

true if this country is a member of the European Union.

Returns:

  • (Boolean)


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

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

#local_nameString

Returns The name for this Country, in this Country’s locale.

Returns:

  • (String)

    The name for this Country, in this Country’s locale.



154
155
156
# File 'lib/countries/country.rb', line 154

def local_name
  @local_name ||= local_names.first
end

#local_namesArray<String>

Returns TThe list of names for this Country, in this Country’s locales.

Returns:

  • (Array<String>)

    TThe list of names for this Country, in this Country’s locales.



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

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

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

#mongoizeObject



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

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

#statesObject



99
100
101
102
103
104
105
106
107
# File 'lib/countries/country.rb', line 99

def states
  if RUBY_VERSION =~ /^3\.\d\.\d/
    warn "DEPRECATION WARNING: The Country#states method has been deprecated and will be removed in 6.0. Please use Country#subdivisions instead.", uplevel: 1, category: :deprecated
  else
    warn "DEPRECATION WARNING: The Country#states method has been deprecated and will be removed in 6.0. Please use Country#subdivisions instead.", uplevel: 1
  end

  subdivisions
end

#subdivision_names(locale = 'en') ⇒ Array<String>

Returns A list of subdivision names for this country.

Parameters:

  • locale (String) (defaults to: 'en')

    The locale to use for translations.

Returns:

  • (Array<String>)

    A list of subdivision names for this country.



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

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

#subdivision_names_with_codes(locale = 'en') ⇒ Array<Array>

Returns This Country’s subdivision pairs of names and codes.

Parameters:

  • locale (String) (defaults to: 'en')

    The locale to use for translations.

Returns:

  • (Array<Array>)

    This Country’s subdivision pairs of names and codes.



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

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

#subdivision_typesArray<String>

Returns the list of subdivision types for this country.

Returns:

  • (Array<String>)

    the list of subdivision types for this country



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

def subdivision_types
  subdivisions.map{|k,v| v['type']}.uniq
end

#subdivisionsArray<ISO3166::Subdivision>

Returns the list of subdivisions for this Country.

Returns:



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

def subdivisions
  @subdivisions ||= if data['subdivisions']
                      ISO3166::Data.create_subdivisions(data['subdivisions'])
                    else
                      ISO3166::Data.subdivisions(alpha2)
                    end
end

#subdivisions?Boolean

true if this Country has any Subdivisions.

Returns:

  • (Boolean)


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

def subdivisions?
  !subdivisions.empty?
end

#subdivisions_of_types(types) ⇒ Array<ISO3166::Subdivision>

Returns the list of subdivisions of the given type(s) for this Country.

Parameters:

  • types (Array<String>)

    The locale to use for translations.

Returns:



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

def subdivisions_of_types(types)
  subdivisions.select{|k,v| types.include?(v.type)}
end

#to_sObject



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

def to_s
  data['iso_short_name']
end

#translated_namesArray<String>

Returns the list of names for this Country in all loaded locales.

Returns:

  • (Array<String>)

    the list of names for this Country in all loaded locales.



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

def translated_names
  data['translations'].values.compact
end

#translation(locale = 'en') ⇒ String

Returns the name of this Country in the selected locale.

Parameters:

  • locale (String) (defaults to: 'en')

    The locale to use for translations.

Returns:

  • (String)

    the name of this Country in the selected locale.



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

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

#valid?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/countries/country.rb', line 27

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