Class: ISO3166::Data

Inherits:
Object
  • Object
show all
Extended by:
LocalesMethods, SubdivisionMethods
Defined in:
lib/countries/data.rb

Overview

Handles building the in memory store of countries data

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SubdivisionMethods

create_subdivisions, load_data_for_alpha2, subdivision_data, subdivisions

Constructor Details

#initialize(alpha2) ⇒ Data

Returns a new instance of Data.



16
17
18
# File 'lib/countries/data.rb', line 16

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

Class Method Details

.cacheObject



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

def cache
  update_cache
end

.codesObject



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

def codes
  load_data!
  cached_codes
end

.datafile_path(file_array) ⇒ Object



72
73
74
# File 'lib/countries/data.rb', line 72

def datafile_path(file_array)
  File.join([@cache_dir] + file_array)
end

.loaded_codesObject



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

def loaded_codes
  load_data!
  @loaded_country_codes
end

.register(data) ⇒ Object

Registers a new Country with custom data. If you are overriding an existing country, this does not perform a deep merge so you will need to __bring in all data you wish to be available__. Overriding an existing country will also remove it from the internal management of translations.



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

def register(data)
  alpha2 = data[:alpha2].upcase
  @registered_data[alpha2] = deep_stringify_keys(data)
  @registered_data[alpha2]['translations'] = Translations.new.merge(data['translations'] || {})
  @cache = cache.merge(@registered_data)
end

.resetObject

Resets the loaded data and cache



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

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

.unregister(alpha2) ⇒ Object

Removes a country from the loaded data



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



61
62
63
64
65
# File 'lib/countries/data.rb', line 61

def update_cache
  load_data!
  sync_translations!
  @cache
end

Instance Method Details

#callObject



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

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