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 11 |
# File 'lib/countries/data.rb', line 8 def initialize(alpha2) @alpha2 = alpha2.to_s.upcase self.class.update_cache end |
Class Method Details
.cache ⇒ Object
33 34 35 |
# File 'lib/countries/data.rb', line 33 def cache update_cache end |
.codes ⇒ Object
43 44 45 46 |
# File 'lib/countries/data.rb', line 43 def codes load_data! loaded_codes end |
.load_data! ⇒ Object
54 55 56 57 58 59 |
# File 'lib/countries/data.rb', line 54 def load_data! return @@cache unless @@cache.size == loaded_codes || @@cache.keys.empty? @@cache = load_cache %w(cache countries.json) @@cache = @@cache.merge(@@registered_data) @@cache end |
.register(data) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/countries/data.rb', line 18 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
37 38 39 40 41 |
# File 'lib/countries/data.rb', line 37 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
27 28 29 30 31 |
# File 'lib/countries/data.rb', line 27 def unregister(alpha2) alpha2 = alpha2.to_s.upcase @@cache.delete(alpha2) @@registered_data.delete(alpha2) end |
.update_cache ⇒ Object
48 49 50 51 52 |
# File 'lib/countries/data.rb', line 48 def update_cache load_data! sync_translations! @@cache end |
Instance Method Details
#call ⇒ Object
13 14 15 |
# File 'lib/countries/data.rb', line 13 def call @@cache[@alpha2] end |