Class: ISO3166::Country

Inherits:
Object
  • Object
show all
Extended by:
CountryClassMethods, CountryFinderMethods
Includes:
CountrySubdivisionMethods, 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, collect_countries_with, collect_likely_countries_by_subdivision_name, new, pluck, translations

Methods included from CountryFinderMethods

[], find_all_by, method_missing, respond_to_missing?, search

Methods included from TimezoneExtensions

#timezones

Methods included from CountrySubdivisionMethods

#find_subdivision_by_name, #humanized_subdivision_types, #states, #subdivision_for_string?, #subdivision_names, #subdivision_names_with_codes, #subdivision_types, #subdivisions, #subdivisions?, #subdivisions_of_types

Methods included from Emoji

#emoji_flag

Constructor Details

#initialize(country_data) ⇒ Country

Returns a new instance of Country.



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

def initialize(country_data)
  @country_data_or_code = country_data
  reload
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

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



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

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

#==(other) ⇒ Object



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

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.



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  self == other
end

#gdpr_compliant?Boolean

true if this country is a member of the European Economic Area or it is UK

Returns:

  • (Boolean)


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

def gdpr_compliant?
  data['eea_member'] || alpha2 == 'GB'
end

#hashObject



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

def hash
  [alpha2, alpha3].hash
end

#in_eea?Boolean

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

Returns:

  • (Boolean)


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

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)


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

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)


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

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

#in_eu_vat?Boolean

true if this country is a member of the EU VAT Area.

Returns:

  • (Boolean)


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

def in_eu_vat?
  data['euvat_member'].nil? ? in_eu? : data['euvat_member']
end

#in_g20?Boolean

true if this country is a member of the G20.

Returns:

  • (Boolean)


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

def in_g20?
  data['g20_member'].nil? ? false : data['g20_member']
end

#in_g7?Boolean

true if this country is a member of the G7.

Returns:

  • (Boolean)


60
61
62
# File 'lib/countries/country.rb', line 60

def in_g7?
  data['g7_member'].nil? ? false : data['g7_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.



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

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.



116
117
118
119
120
121
# File 'lib/countries/country.rb', line 116

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

#postal_code_formatString

Returns The regex for valid postal codes in this Country.

Returns:

  • (String)

    The regex for valid postal codes in this Country



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

def postal_code_format
  "\\A#{data['postal_code_format']}\\Z" if postal_code
end

#to_sObject



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

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.



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

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.



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

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

#valid?Boolean

Returns:

  • (Boolean)


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

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

#zip_formatObject



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

alias zip_format postal_code_format