class Mobility::Plugins::Fallbacks::BackendReader

Public Class Methods

new(fallbacks_option, fallbacks_generator) click to toggle source
# File lib/mobility/plugins/fallbacks.rb, line 138
def initialize(fallbacks_option, fallbacks_generator)
  @fallbacks_generator = fallbacks_generator
  define_read(convert_option_to_fallbacks(fallbacks_option))
end

Private Instance Methods

convert_option_to_fallbacks(option) click to toggle source
# File lib/mobility/plugins/fallbacks.rb, line 159
def convert_option_to_fallbacks(option)
  if option.is_a?(::Hash)
    @fallbacks_generator[option]
  elsif option == true
    @fallbacks_generator[{}]
  else
    ::Hash.new { [] }
  end
end
define_read(fallbacks) click to toggle source
Calls superclass method
# File lib/mobility/plugins/fallbacks.rb, line 145
def define_read(fallbacks)
  define_method :read do |locale, fallback: true, **options|
    return super(locale, **options) if !fallback || options[:locale]

    locales = fallback == true ? fallbacks[locale] : [locale, *fallback]
    locales.each do |fallback_locale|
      value = super(fallback_locale, **options)
      return value if Util.present?(value)
    end

    super(locale, **options)
  end
end