Class: ISO3166::Data
- Inherits:
-
Object
- Object
- ISO3166::Data
- Defined in:
- lib/countries/data.rb
Overview
Handles building the in memory store of countries data
Class Method Summary collapse
- .cache ⇒ Object
- .codes ⇒ Object
- .create_subdivisions(subdivision_data) ⇒ Object
- .datafile_path(file_array) ⇒ Object
- .loaded_codes ⇒ Object
-
.register(data) ⇒ Object
Registers a new Country with custom data.
-
.reset ⇒ Object
Resets the loaded data and cache.
- .subdivision_data(alpha2) ⇒ Object
- .subdivisions(alpha2) ⇒ Object
-
.unregister(alpha2) ⇒ Object
Removes a country from the loaded data.
- .update_cache ⇒ Object
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(alpha2) ⇒ Data
constructor
A new instance of Data.
Constructor Details
#initialize(alpha2) ⇒ Data
Returns a new instance of Data.
13 14 15 |
# File 'lib/countries/data.rb', line 13 def initialize(alpha2) @alpha2 = alpha2.to_s.upcase end |
Class Method Details
.cache ⇒ Object
41 42 43 |
# File 'lib/countries/data.rb', line 41 def cache update_cache end |
.codes ⇒ Object
53 54 55 56 |
# File 'lib/countries/data.rb', line 53 def codes load_data! cached_codes end |
.create_subdivisions(subdivision_data) ⇒ Object
87 88 89 90 91 |
# File 'lib/countries/data.rb', line 87 def create_subdivisions(subdivision_data) subdivision_data.each_with_object({}) do |(k, v), hash| hash[k] = Subdivision.new(v) end end |
.datafile_path(file_array) ⇒ Object
69 70 71 |
# File 'lib/countries/data.rb', line 69 def datafile_path(file_array) File.join([@cache_dir] + file_array) end |
.loaded_codes ⇒ Object
64 65 66 67 |
# File 'lib/countries/data.rb', line 64 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.
26 27 28 29 30 31 32 |
# File 'lib/countries/data.rb', line 26 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 |
.reset ⇒ Object
Resets the loaded data and cache
46 47 48 49 50 51 |
# File 'lib/countries/data.rb', line 46 def reset @cache = {} @subdivisions = {} @registered_data = {} ISO3166.configuration.loaded_locales = [] end |
.subdivision_data(alpha2) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/countries/data.rb', line 73 def subdivision_data(alpha2) file = subdivision_file_path(alpha2) data = File.exist?(file) ? YAML.load_file(file) : {} locales = ISO3166.configuration.locales.map(&:to_s) data.each_value{ |v| v['translations'] = v['translations'].slice(*locales)} return data end |
.subdivisions(alpha2) ⇒ Object
82 83 84 85 |
# File 'lib/countries/data.rb', line 82 def subdivisions(alpha2) @subdivisions ||= {} @subdivisions[alpha2] ||= create_subdivisions(ISO3166::Data.subdivision_data(alpha2)) end |
.unregister(alpha2) ⇒ Object
Removes a country from the loaded data
35 36 37 38 39 |
# File 'lib/countries/data.rb', line 35 def unregister(alpha2) alpha2 = alpha2.to_s.upcase @cache.delete(alpha2) @registered_data.delete(alpha2) end |
.update_cache ⇒ Object
58 59 60 61 62 |
# File 'lib/countries/data.rb', line 58 def update_cache load_data! sync_translations! @cache end |
Instance Method Details
#call ⇒ Object
17 18 19 |
# File 'lib/countries/data.rb', line 17 def call self.class.update_cache[@alpha2] end |