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_dir =
[File.dirname(__FILE__), 'cache']
@@cache =
{}
@@registered_data =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alpha2) ⇒ Data

Returns a new instance of Data


9
10
11
# File 'lib/countries/data.rb', line 9

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

Class Method Details

.cacheObject


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

def cache
  update_cache
end

.cache_dirObject


18
19
20
# File 'lib/countries/data.rb', line 18

def cache_dir
  @@cache_dir
end

.cache_dir=(value) ⇒ Object


22
23
24
# File 'lib/countries/data.rb', line 22

def cache_dir=(value)
  @@cache_dir = value
end

.codesObject


51
52
53
54
# File 'lib/countries/data.rb', line 51

def codes
  load_data!
  loaded_codes
end

.load_data!Object


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

def load_data!
  return @@cache unless load_required?
  @@cache = load_cache %w(countries.json)
  @@_country_codes = @@cache.keys
  @@cache = @@cache.merge(@@registered_data)
  @@cache
end

.register(data) ⇒ Object


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

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


45
46
47
48
49
# File 'lib/countries/data.rb', line 45

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

.sync_translations!Object


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/countries/data.rb', line 70

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


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_cacheObject


56
57
58
59
60
# File 'lib/countries/data.rb', line 56

def update_cache
  load_data!
  sync_translations!
  @@cache
end

Instance Method Details

#callObject


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

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