Class: CountriesData::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/countries_data/collection.rb

Constant Summary collapse

DATA =
YAML.safe_load_file(File.join(__dir__, 'data.yml'), symbolize_names: true).freeze
COUNTRIES =
DATA[:countries].map { |c| Country.new(c) }.freeze
IDS_MAP =
COUNTRIES.each_with_index.with_object({}) do |(country, index), prev|
  prev[country.id] = index
end
PHONE_CODES_MAP =
COUNTRIES.each_with_index.with_object({}) do |(country, index), prev|
  next prev if country.phone_code.nil?

  prev[country.phone_code] = [] if prev[country.phone_code].nil?
  prev[country.phone_code].push(index)
end

Class Method Summary collapse

Class Method Details

.allObject



21
22
23
# File 'lib/countries_data/collection.rb', line 21

def self.all
  COUNTRIES
end

.find_by_id(id) ⇒ Object



25
26
27
28
# File 'lib/countries_data/collection.rb', line 25

def self.find_by_id(id)
  index = IDS_MAP[id.to_s]
  COUNTRIES[index] unless index.nil?
end

.find_by_phone_code(phone_code) ⇒ Object



30
31
32
33
# File 'lib/countries_data/collection.rb', line 30

def self.find_by_phone_code(phone_code)
  indexes = PHONE_CODES_MAP[phone_code] || []
  indexes.map { |index| COUNTRIES[index] }
end