module Mobility::Plugins::Backend

Plugin for setting up a backend for a set of model attributes. All backend plugins must depend on this.

Defines:

Attributes

backend[R]

Backend @return [Symbol,Class,Class] Name of backend, or backend class

backend_class[R]

Backend class @return [Class] Backend class

backend_options[R]

Backend options @return [Hash] Options for backend

Public Class Methods

new(*args, **original_options) click to toggle source
Calls superclass method
# File lib/mobility/plugins/backend.rb, line 33
def initialize(*args, **original_options)
  super

  include InstanceMethods
end

Private Class Methods

configure_default(defaults, key, backend = nil, backend_options = {}) click to toggle source

Override default argument-handling in DSL to store kwargs passed along with plugin name.

# File lib/mobility/plugins/backend.rb, line 113
def self.configure_default(defaults, key, backend = nil, backend_options = {})
  defaults[key] = [backend, backend_options] if backend
end

Public Instance Methods

included(klass) click to toggle source

Setup backend class, include modules into model class, include/extend shared modules and setup model with backend setup block (see {Mobility::Backend::Setup#setup_model}).

Calls superclass method
# File lib/mobility/plugins/backend.rb, line 42
def included(klass)
  super

  klass.extend ClassMethods

  if backend
    @backend_class = backend.build_subclass(klass, backend_options)

    backend_class.setup_model(klass, names)

    names = @names
    backend_class = @backend_class

    klass.class_eval do
      names.each { |name| mobility_backend_classes[name.to_sym] = backend_class }
    end

    backend_class
  end
end
inspect() click to toggle source

Include backend name in inspect string. @return [String]

# File lib/mobility/plugins/backend.rb, line 65
def inspect
  "#<Translations (#{backend}) @names=#{names.join(", ")}>"
end
load_backend(backend) click to toggle source
# File lib/mobility/plugins/backend.rb, line 69
def load_backend(backend)
  Backends.load_backend(backend)
rescue Backends::LoadError => e
  raise e, "could not find a #{backend} backend. Did you forget to include an ORM plugin like active_record or sequel?"
end

Private Instance Methods

initialize_options(original_options) click to toggle source

Override to extract backend options from options hash.

Calls superclass method
# File lib/mobility/plugins/backend.rb, line 78
def initialize_options(original_options)
  super

  case options[:backend]
  when String, Symbol, Class
    @backend, @backend_options = options[:backend], options.dup
  when Array
    @backend, @backend_options = options[:backend]
    @backend_options = @backend_options.merge(options)
  when NilClass
    @backend = @backend_options = nil
  else
    raise ArgumentError, "backend must be either a backend name, a backend class, or a two-element array"
  end

  @backend = load_backend(backend)
end
validate_options(options) click to toggle source

Override default validation to exclude backend options, which may be mixed in with plugin options.

Calls superclass method
# File lib/mobility/plugins/backend.rb, line 98
def validate_options(options)
  return super unless backend
  super(options.slice(*(options.keys - backend.valid_keys)))

  # Validate that the default backend from config has valid keys, or if
  # it is overridden by an array input that the array has valid keys.
  if options[:backend].is_a?(Array)
    name, backend_options = options[:backend]
    extra_keys = backend_options.keys - backend.valid_keys
    raise InvalidOptionKey, "These are not valid #{name} backend keys: #{extra_keys.join(', ')}." unless extra_keys.empty?
  end
end