Module: Mobility::Plugins::Backend

Extended by:
Mobility::Plugin
Defined in:
lib/mobility/plugins/backend.rb

Overview

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.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods Classes: InvalidOptionKey, MobilityBackends

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mobility::Plugin

configure, configure_default, default, dependencies, dependencies_satisfied?, included_hook, initialize_hook, requires

Instance Attribute Details

#backendSymbol, Class (readonly)

Backend

Returns:

  • (Symbol, Class, Class)

    Name of backend, or backend class



27
28
29
# File 'lib/mobility/plugins/backend.rb', line 27

def backend
  @backend
end

#backend_classClass (readonly)

Backend class

Returns:

  • (Class)

    Backend class



23
24
25
# File 'lib/mobility/plugins/backend.rb', line 23

def backend_class
  @backend_class
end

#backend_optionsHash (readonly)

Backend options

Returns:

  • (Hash)

    Options for backend



31
32
33
# File 'lib/mobility/plugins/backend.rb', line 31

def backend_options
  @backend_options
end

Instance Method Details

#included(klass) ⇒ Object

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

#initialize(*args, **original_options) ⇒ Object



33
34
35
36
37
# File 'lib/mobility/plugins/backend.rb', line 33

def initialize(*args, **original_options)
  super

  include InstanceMethods
end

#inspectString

Include backend name in inspect string.

Returns:

  • (String)


65
66
67
# File 'lib/mobility/plugins/backend.rb', line 65

def inspect
  "#<Translations (#{backend}) @names=#{names.join(", ")}>"
end

#load_backend(backend) ⇒ Object



69
70
71
72
73
# 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