Module: Kountries::Europe

Defined in:
lib/kountries/europe.rb

Overview

Europe

Defined Under Namespace

Classes: AD, AL, AT, AX, BA, BE, BG, BY, CH, CZ, DE, DK, EE, ES, FI, FO, FR, GB, GG, GI, GR, HR, HU, IE, IM, IS, IT, JE, LI, LT, LU, LV, MC, MD, ME, MK, MT, NL, NO, PL, PT, RO, RS, RU, SE, SI, SJ, SK, SM, UA, VA

Class Method Summary collapse

Class Method Details

.as_struct(european_country_class) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/kountries/europe.rb', line 25

def self.as_struct(european_country_class)
  OpenStruct.new(
    alpha2: european_country_class::ALPHA_2,
    alpha3: european_country_class::ALPHA_3,
    country_code: european_country_class::COUNTRY_CODE,
    name: european_country_class::NAME,
    unofficial_names: european_country_class::UN_OFFICIAL_NAMES
  )
end

.empty_structObject



35
36
37
38
39
40
41
42
43
# File 'lib/kountries/europe.rb', line 35

def self.empty_struct
  OpenStruct.new(
    alpha2: nil,
    alpha3: nil,
    country_code: nil,
    name: nil,
    unofficial_names: nil
  )
end

.find_country_by_code(code) ⇒ Object



18
19
20
21
22
23
# File 'lib/kountries/europe.rb', line 18

def self.find_country_by_code(code)
  return empty_struct unless Kountries::Europe.constants.include?(code.intern)

  european_country_class = Module.const_get "Kountries::Europe::#{code}"
  return as_struct(european_country_class) if european_country_class::ALPHA_2 == code
end

.find_country_by_name(name) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/kountries/europe.rb', line 8

def self.find_country_by_name(name)
  constants.each do |country|
    european_country_class = Module.const_get "Kountries::Europe::#{country}"
    return as_struct(european_country_class) if european_country_class::NAME.upcase == name.upcase
    return as_struct(european_country_class) if european_country_class::UN_OFFICIAL_NAMES.include?(name.capitalize)
  end

  empty_struct
end