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 =
{}
@@semaphore =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alpha2) ⇒ Data



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

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

Class Method Details

.cacheObject



42
43
44
45
46
# File 'lib/countries/data.rb', line 42

def cache
  @@semaphore.synchronize do
    update_cache
  end
end

.cache_dirObject



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

def cache_dir
  @@cache_dir
end

.cache_dir=(value) ⇒ Object



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

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

.codesObject



54
55
56
57
# File 'lib/countries/data.rb', line 54

def codes
  load_data!
  loaded_codes
end

.load_data!Object



65
66
67
68
69
70
71
# File 'lib/countries/data.rb', line 65

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



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

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



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

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

.sync_translations!Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/countries/data.rb', line 73

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



36
37
38
39
40
# File 'lib/countries/data.rb', line 36

def unregister(alpha2)
  alpha2 = alpha2.to_s.upcase
  @@cache.delete(alpha2)
  @@registered_data.delete(alpha2)
end

.update_cacheObject



59
60
61
62
63
# File 'lib/countries/data.rb', line 59

def update_cache
  load_data!
  sync_translations!
  @@cache
end

Instance Method Details

#callObject



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

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