class ISO3166::Data

Handles building the in memory store of countries data

Public Class Methods

cache() click to toggle source
# File lib/countries/data.rb, line 41
def cache
  update_cache
end
cache_dir() click to toggle source
# File lib/countries/data.rb, line 19
def cache_dir
  @@cache_dir
end
cache_dir=(value) click to toggle source
# File lib/countries/data.rb, line 23
def cache_dir=(value)
  @@cache_dir = value
end
codes() click to toggle source
# File lib/countries/data.rb, line 51
def codes
  load_data!
  loaded_codes
end
load_data!() click to toggle source
# File lib/countries/data.rb, line 62
def load_data!
  return @@cache unless load_required?
  synchronized do
    @@cache = load_cache %w(countries.json)
    @@_country_codes = @@cache.keys
    @@cache = @@cache.merge(@@registered_data)
    @@cache
  end
end
new(alpha2) click to toggle source
# File lib/countries/data.rb, line 10
def initialize(alpha2)
  @alpha2 = alpha2.to_s.upcase
end
register(data) click to toggle source
# File lib/countries/data.rb, line 27
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
reset() click to toggle source
# File lib/countries/data.rb, line 45
def reset
  @@cache = {}
  @@registered_data = {}
  ISO3166.configuration.loaded_locales = []
end
sync_translations!() click to toggle source
# File lib/countries/data.rb, line 72
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) click to toggle source
# File lib/countries/data.rb, line 35
def unregister(alpha2)
  alpha2 = alpha2.to_s.upcase
  @@cache.delete(alpha2)
  @@registered_data.delete(alpha2)
end
update_cache() click to toggle source
# File lib/countries/data.rb, line 56
def update_cache
  load_data!
  sync_translations!
  @@cache
end

Private Class Methods

cache_flush_required?() click to toggle source
# File lib/countries/data.rb, line 114
def cache_flush_required?
  !locales_to_load.empty? || !locales_to_remove.empty?
end
datafile_path(file_array) click to toggle source
# File lib/countries/data.rb, line 161
def datafile_path(file_array)
  File.join([@@cache_dir] + file_array)
end
deep_stringify_keys(data) click to toggle source
# File lib/countries/data.rb, line 165
def deep_stringify_keys(data)
  data.transform_keys!(&:to_s)
  data.transform_values! do |v|
    v.is_a?(Hash) ? deep_stringify_keys(v) : v
  end
  return data
end
internal_codes() click to toggle source

Codes that we have translations for in dataset

# File lib/countries/data.rb, line 110
def internal_codes
  @@_country_codes - @@registered_data.keys
end
load_cache(file_array) click to toggle source
# File lib/countries/data.rb, line 156
def load_cache(file_array)
  file_path = datafile_path(file_array)
  File.exist?(file_path) ? JSON.parse(File.binread(file_path)) : {}
end
load_required?() click to toggle source
# File lib/countries/data.rb, line 99
def load_required?
  synchronized do
    @@cache.empty?
  end
end
load_translations(locale) click to toggle source
# File lib/countries/data.rb, line 134
def load_translations(locale)
  synchronized do
    locale_names = load_cache(['locales', "#{locale}.json"])
    internal_codes.each do |alpha2|
      @@cache[alpha2]['translations'] ||= Translations.new
      @@cache[alpha2]['translations'][locale] = locale_names[alpha2].freeze
      @@cache[alpha2]['translated_names'] = @@cache[alpha2]['translations'].values.freeze
    end
    ISO3166.configuration.loaded_locales << locale
  end
end
loaded_codes() click to toggle source
# File lib/countries/data.rb, line 105
def loaded_codes
  @@cache.keys
end
loaded_locales() click to toggle source
# File lib/countries/data.rb, line 130
def loaded_locales
  ISO3166.configuration.loaded_locales.map { |l| l.to_s.downcase }
end
locales_to_load() click to toggle source
# File lib/countries/data.rb, line 118
def locales_to_load
  requested_locales - loaded_locales
end
locales_to_remove() click to toggle source
# File lib/countries/data.rb, line 122
def locales_to_remove
  loaded_locales - requested_locales
end
requested_locales() click to toggle source
# File lib/countries/data.rb, line 126
def requested_locales
  ISO3166.configuration.locales.map { |l| l.to_s.downcase }
end
synchronized(&block) click to toggle source
# File lib/countries/data.rb, line 86
def synchronized(&block)
  if use_mutex?
    @@mutex.synchronize(&block)
  else
    block.call
  end
end
unload_translations(locale) click to toggle source
# File lib/countries/data.rb, line 146
def unload_translations(locale)
  synchronized do
    internal_codes.each do |alpha2|
      @@cache[alpha2]['translations'].delete(locale)
      @@cache[alpha2]['translated_names'] = @@cache[alpha2]['translations'].values.freeze
    end
    ISO3166.configuration.loaded_locales.delete(locale)
  end
end
use_mutex?() click to toggle source
# File lib/countries/data.rb, line 94
def use_mutex?
  # Stubbed in testing
  true
end

Public Instance Methods

call() click to toggle source
# File lib/countries/data.rb, line 14
def call
  self.class.update_cache[@alpha2]
end