Class: ISO3166::Data

Inherits:
Object
  • Object
show all
Extended by:
LocalesMethods, SubdivisionMethods
Defined in:
lib/countries/data.rb

Overview

Handles building the in memory store of countries data

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SubdivisionMethods

create_subdivisions, load_data_for_alpha2, subdivision_data, subdivisions

Constructor Details

#initialize(alpha2) ⇒ Data

Returns a new instance of Data.



16
17
18
# File 'lib/countries/data.rb', line 16

def initialize(alpha2)
  @alpha2 = alpha2.to_s.upcase
end

Class Method Details

.cacheObject



42
43
44
# File 'lib/countries/data.rb', line 42

def cache
  update_cache
end

.codesObject



54
55
56
57
# File 'lib/countries/data.rb', line 54

def codes
  load_data!
  cached_codes
end

.datafile_path(file_array) ⇒ Object



70
71
72
# File 'lib/countries/data.rb', line 70

def datafile_path(file_array)
  File.join([@cache_dir] + file_array)
end

.loaded_codesObject



65
66
67
68
# File 'lib/countries/data.rb', line 65

def loaded_codes
  load_data!
  @loaded_country_codes
end

.register(data) ⇒ Object

Registers a new Country with custom data. If you are overriding an existing country, this does not perform a deep merge so you will need to __bring in all data you wish to be available__. Overriding an existing country will also remove it from the internal management of translations.



28
29
30
31
32
33
# File 'lib/countries/data.rb', line 28

def register(data)
  alpha2 = data[:alpha2].upcase
  @registered_data[alpha2] = deep_stringify_keys(data)
  @registered_data[alpha2]['translations'] = Translations.new.merge(data['translations'] || {})
  @cache = cache.merge(@registered_data)
end

.resetObject

Resets the loaded data and cache



47
48
49
50
51
52
# File 'lib/countries/data.rb', line 47

def reset
  @cache = {}
  @subdivisions = {}
  @registered_data = {}
  ISO3166.configuration.loaded_locales = []
end

.unregister(alpha2) ⇒ Object

Removes a country from the loaded data



36
37
38
39
40
# File 'lib/countries/data.rb', line 36

def unregister(alpha2)
  alpha2 = alpha2.to_s.upcase
  @cache.delete(alpha2)
  @registered_data.delete(alpha2)
end

.update_cacheObject



59
60
61
62
63
# File 'lib/countries/data.rb', line 59

def update_cache
  load_data!
  sync_translations!
  @cache
end

Instance Method Details

#callObject



20
21
22
# File 'lib/countries/data.rb', line 20

def call
  self.class.update_cache[@alpha2]
end