Class: ISO3166::Data
- Inherits:
-
Object
- Object
- ISO3166::Data
- Defined in:
- lib/countries/data.rb
Overview
Handles building the in memory store of countries data
Constant Summary collapse
- @@cache =
{}
- @@registered_data =
{}
Class Method Summary collapse
- .cache ⇒ Object
- .codes ⇒ Object
- .load_data! ⇒ Object
- .register(data) ⇒ Object
- .reset ⇒ Object
- .sync_translations! ⇒ Object
- .unregister(alpha2) ⇒ Object
- .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.
8 9 10 |
# File 'lib/countries/data.rb', line 8 def initialize(alpha2) @alpha2 = alpha2.to_s.upcase end |
Class Method Details
.cache ⇒ Object
32 33 34 |
# File 'lib/countries/data.rb', line 32 def cache update_cache end |
.codes ⇒ Object
42 43 44 45 |
# File 'lib/countries/data.rb', line 42 def codes load_data! loaded_codes end |
.load_data! ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/countries/data.rb', line 53 def load_data! return @@cache unless load_required? @@cache = load_cache %w(cache countries.json) @@_country_codes = @@cache.keys @@cache = @@cache.merge(@@registered_data) @@cache end |
.register(data) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/countries/data.rb', line 17 def register(data) alpha2 = data[:alpha2].upcase @@registered_data[alpha2] = \ data.each_with_object({}) { |(k, v), a| a[k.to_s] = v } @@registered_data[alpha2]['translations'] = \ Translations.new.merge(data[:translations] || {}) @@cache = cache.merge(@@registered_data) end |
.reset ⇒ Object
36 37 38 39 40 |
# File 'lib/countries/data.rb', line 36 def reset @@cache = {} @@registered_data = {} ISO3166.configuration.loaded_locales = [] end |
.sync_translations! ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/countries/data.rb', line 61 def sync_translations! return unless cache_flush_required? locales_to_remove.each do |locale| unload_translations(locale) end locales_to_load.each do |locale| load_translations(locale) end end |
.unregister(alpha2) ⇒ Object
26 27 28 29 30 |
# File 'lib/countries/data.rb', line 26 def unregister(alpha2) alpha2 = alpha2.to_s.upcase @@cache.delete(alpha2) @@registered_data.delete(alpha2) end |
.update_cache ⇒ Object
47 48 49 50 51 |
# File 'lib/countries/data.rb', line 47 def update_cache load_data! sync_translations! @@cache end |
Instance Method Details
#call ⇒ Object
12 13 14 |
# File 'lib/countries/data.rb', line 12 def call self.class.update_cache[@alpha2] end |