module Mobility::Plugins::Backend
Plugin
for setting up a backend for a set of model attributes. All backend plugins must depend on this.
Defines:
-
instance method
mobility_backends
which returns a hash whose keys are attribute names and values a backend for each attribute. -
class method
mobility_backend_class
which takes an attribute name and returns the backend class for that name.
Attributes
Backend
@return [Symbol,Class,Class] Name of backend, or backend class
Backend
options @return [Hash] Options for backend
Public Class Methods
# File lib/mobility/plugins/backend.rb, line 33 def initialize(*args, **original_options) super include InstanceMethods end
Private Class Methods
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
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}).
# 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
Include backend name in inspect string. @return [String]
# File lib/mobility/plugins/backend.rb, line 65 def inspect "#<Translations (#{backend}) @names=#{names.join(", ")}>" end
# 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
Override to extract backend options from options hash.
# 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
Override default validation to exclude backend options, which may be mixed in with plugin options.
# 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