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
19
20
21
# File 'lib/countries/data.rb', line 16

def initialize(alpha2)
  alpha2 = alpha2.to_s if alpha2.is_a?(Symbol)
  alpha2 = alpha2.upcase if alpha2.match?(/[a-z]/)

  @alpha2 = alpha2
end

Class Method Details

.cacheObject



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

def cache
  update_cache
end

.codesObject



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

def codes
  load_data!
  cached_codes
end

.datafile_path(file_array) ⇒ Object



77
78
79
# File 'lib/countries/data.rb', line 77

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

.loaded_codesObject



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

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.



31
32
33
34
35
36
37
38
# File 'lib/countries/data.rb', line 31

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

.resetObject

Resets the loaded data and cache



52
53
54
55
56
57
58
59
# File 'lib/countries/data.rb', line 52

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

.unregister(alpha2) ⇒ Object

Removes a country from the loaded data



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

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

.update_cacheObject



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

def update_cache
  load_data!
  sync_translations!
  @cache
end

Instance Method Details

#callObject



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

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