Module: CountryTools

Defined in:
lib/country_tools.rb

Constant Summary collapse

EU_CODES =
[
  'AT',       # Austria
  'BE',       # Belgium
  'BG',       # Bulgaria
  'HR',       # Croatia
  'CY',       # Cyprus
  'CZ',       # Czech Republic
  'DK',       # Denmark
  'EE',       # Estonia
  'FI',       # Finland
  'FR',       # France
  'DE',       # Germany
  'GR', 'EL', # Greece (https://fr.wikipedia.org/wiki/Union_europ%C3%A9enne#cite_ref-79)
  'HU',       # Hungary
  'IE',       # Ireland
  'IT',       # Italy
  'LV',       # Latvia
  'LT',       # Lithuania
  'LU',       # Luxembourg
  'MT',       # Malta
  'NL',       # Netherlands
  'PL',       # Poland
  'PT',       # Portugal
  'RO',       # Romania
  'SK',       # Slovakia
  'SI',       # Slovenia
  'ES',       # Spain
  'SE',       # Sweden
]

Class Method Summary collapse

Class Method Details

.all_codesObject



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

def self.all_codes
  @@all_codes ||= all_countries.keys.sort
end

.all_countriesObject



2
3
4
5
# File 'lib/country_tools.rb', line 2

def self.all_countries
  path = File.join(File.dirname(File.dirname(__FILE__)), 'names.yml')
  @@all_countries ||= YAML.load(File.read(path))
end

.in_cee?(code) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/country_tools.rb', line 42

def self.in_cee?(code)
  warn "[DEPRECATION] `in_cee?` is deprecated. Please use `in_eu?` instead."
  in_eu?(code)
end

.in_eu?(code) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.in_eu?(code)
  EU_CODES.include?(code)
end

.name(code) ⇒ Object



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

def self.name(code)
  if all_countries.has_key?(code.to_s)
    all_countries[code.to_s][I18n.locale.to_s]
  else
    code
  end
end

.options_for_selectObject



47
48
49
50
51
52
53
# File 'lib/country_tools.rb', line 47

def self.options_for_select
  all_countries.collect do |code, names|
    [names[I18n.locale.to_s], code]
  end.sort do |a, b|
    a.first <=> b.first
  end
end