Class: ISO3166::Data

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

.cacheObject



32
33
34
# File 'lib/countries/data.rb', line 32

def cache
  update_cache
end

.codesObject



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

.resetObject



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_cacheObject



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

def update_cache
  load_data!
  sync_translations!
  @@cache
end

Instance Method Details

#callObject



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

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