Module: Kountries::Africa

Defined in:
lib/kountries/africa.rb

Overview

Africa

Defined Under Namespace

Classes: AO, BF, BI, BJ, BW, CD, CF, CG, CI, CM, CV, DJ, DZ, EG, EH, ER, ET, GA, GH, GM, GN, GQ, GW, IO, KE, KM, LR, LS, LY, MA, MG, ML, MR, MU, MW, MZ, NA, NE, NG, RE, RW, SC, SD, SH, SL, SN, SO, SS, ST, SZ, TD, TG, TN, TZ, UG, YT, ZA, ZM, ZW

Class Method Summary collapse

Class Method Details

.as_struct(african_country_class) ⇒ Object



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

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

.empty_structObject



35
36
37
38
39
40
41
42
43
# File 'lib/kountries/africa.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/africa.rb', line 18

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

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

.find_country_by_name(name) ⇒ Object



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

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

  empty_struct
end