module Countrizable

Helper class for storing values per country_codes. Used by Countrizable::Adapter to stash and cache attribute values.

Constants

VERSION

Public Class Methods

country_code() click to toggle source
# File lib/countrizable.rb, line 16
def country_code
  read_country_code || 'es'
end
country_code=(country_code) click to toggle source
# File lib/countrizable.rb, line 20
def country_code=(country_code)
  set_country_code(country_code)
end
default_fallbacks(for_country_code = self.country_code) click to toggle source
# File lib/countrizable.rb, line 49
def default_fallbacks(for_country_code = self.country_code)
  [for_country_code.to_sym]
end
fallbacks(for_country_code = self.country_code) click to toggle source
# File lib/countrizable.rb, line 45
def fallbacks(for_country_code = self.country_code)
  read_fallbacks[for_country_code] || default_fallbacks(for_country_code)
end
fallbacks=(country_codes) click to toggle source
# File lib/countrizable.rb, line 41
def fallbacks=(country_codes)
  set_fallbacks(country_codes)
end
rails_52?() click to toggle source
# File lib/countrizable.rb, line 62
def rails_52?
  ::ActiveRecord.version >= Gem::Version.new('5.2.0')
end
rails_5?() click to toggle source
# File lib/countrizable.rb, line 58
def rails_5?
  ::ActiveRecord.version >= Gem::Version.new('5.1.0')
end
storage() click to toggle source

Thread-safe global storage

# File lib/countrizable.rb, line 54
def storage
  RequestStore.store
end
with_country_code(country_code) { |country_code| ... } click to toggle source
# File lib/countrizable.rb, line 24
def with_country_code(country_code, &block)
  previous_country_code = read_country_code
  begin
    set_country_code(country_code)
    result = yield(country_code)
  ensure
    set_country_code(previous_country_code)
  end
  result
end
with_country_codes(*country_codes, &block) click to toggle source
# File lib/countrizable.rb, line 35
def with_country_codes(*country_codes, &block)
  country_codes.flatten.map do |country_code|
    with_country_code(country_code, &block)
  end
end

Protected Class Methods

read_country_code() click to toggle source
# File lib/countrizable.rb, line 68
def read_country_code
  storage[:countrizable_country_code]
end
read_fallbacks() click to toggle source
# File lib/countrizable.rb, line 76
def read_fallbacks
  storage[:countrizable_fallbacks] || HashWithIndifferentAccess.new
end
set_country_code(country_code) click to toggle source
# File lib/countrizable.rb, line 72
def set_country_code(country_code)
  storage[:countrizable_country_code] = country_code.try(:to_sym)
end
set_fallbacks(country_codes) click to toggle source
# File lib/countrizable.rb, line 80
def set_fallbacks(country_codes)
  fallback_hash = HashWithIndifferentAccess.new

  country_codes.each do |key, value|
    fallback_hash[key] = value.presence || [key]
  end if country_codes.present?

  storage[:countrizable_country_code] = fallback_hash
end