Class: CountryList::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/country_list/data.rb

Constant Summary collapse

@@country_codes =
nil
@@cached_countries =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(country_code) ⇒ Data

Returns a new instance of Data.



7
8
9
10
# File 'lib/country_list/data.rb', line 7

def initialize(country_code)
  @country_code = country_code.to_s.upcase
  Data.load_cached_countries unless @@cached_countries
end

Class Method Details

.country_codesObject



22
23
24
# File 'lib/country_list/data.rb', line 22

def country_codes
  @@country_codes ||= load_country_codes!
end

.datafile_path(file_array) ⇒ Object



18
19
20
# File 'lib/country_list/data.rb', line 18

def datafile_path(file_array)
  File.join([File.dirname(__FILE__)] + file_array)
end

.load_cached_countriesObject Also known as: countries



26
27
28
# File 'lib/country_list/data.rb', line 26

def load_cached_countries
  @@cached_countries ||= load_countries!
end

.load_countries!Object



31
32
33
# File 'lib/country_list/data.rb', line 31

def load_countries!
  Marshal.load(File.binread(datafile_path(%w(cache countries ))))
end

.load_country_codes!Object



35
36
37
# File 'lib/country_list/data.rb', line 35

def load_country_codes!
  YAML.load_file(datafile_path(%w(country_data country_codes.yaml)))
end

Instance Method Details

#callObject



12
13
14
# File 'lib/country_list/data.rb', line 12

def call
  @@cached_countries[@country_code]
end