Module: Mobility::Backend

Included in:
ActiveRecord::Column, ActiveRecord::HashValued, ActiveRecord::KeyValue, ActiveRecord::Serialized, ActiveRecord::Table, Null, Sequel::Column, Sequel::HashValued, Sequel::KeyValue, Sequel::Serialized, Sequel::Table
Defined in:
lib/mobility/backend.rb,
lib/mobility/backend/null.rb,
lib/mobility/backend/cache.rb,
lib/mobility/backend/dirty.rb,
lib/mobility/backend/jsonb.rb,
lib/mobility/backend/table.rb,
lib/mobility/backend/column.rb,
lib/mobility/backend/hstore.rb,
lib/mobility/backend/sequel.rb,
lib/mobility/backend/fallbacks.rb,
lib/mobility/backend/key_value.rb,
lib/mobility/backend/serialized.rb,
lib/mobility/backend/active_model.rb,
lib/mobility/backend/sequel/dirty.rb,
lib/mobility/backend/sequel/jsonb.rb,
lib/mobility/backend/sequel/table.rb,
lib/mobility/backend/active_record.rb,
lib/mobility/backend/orm_delegator.rb,
lib/mobility/backend/sequel/column.rb,
lib/mobility/backend/sequel/hstore.rb,
lib/mobility/backend/sequel/key_value.rb,
lib/mobility/backend/sequel/serialized.rb,
lib/mobility/backend/active_model/dirty.rb,
lib/mobility/backend/sequel/hash_valued.rb,
lib/mobility/backend/active_record/jsonb.rb,
lib/mobility/backend/active_record/table.rb,
lib/mobility/backend/active_record/column.rb,
lib/mobility/backend/active_record/hstore.rb,
lib/mobility/backend/sequel/query_methods.rb,
lib/mobility/backend/active_record/key_value.rb,
lib/mobility/backend/active_record/serialized.rb,
lib/mobility/backend/active_record/hash_valued.rb,
lib/mobility/backend/sequel/jsonb/query_methods.rb,
lib/mobility/backend/sequel/table/query_methods.rb,
lib/mobility/backend/active_record/query_methods.rb,
lib/mobility/backend/sequel/column/query_methods.rb,
lib/mobility/backend/sequel/hstore/query_methods.rb,
lib/mobility/backend/sequel/key_value/query_methods.rb,
lib/mobility/backend/sequel/serialized/query_methods.rb,
lib/mobility/backend/active_record/jsonb/query_methods.rb,
lib/mobility/backend/active_record/table/query_methods.rb,
lib/mobility/backend/active_record/column/query_methods.rb,
lib/mobility/backend/active_record/hstore/query_methods.rb,
lib/mobility/backend/active_record/key_value/query_methods.rb,
lib/mobility/backend/active_record/serialized/query_methods.rb

Overview

Defines a minimum set of shared components included in any backend. These are:

  • a reader returning the model on which the backend is defined (#model)

  • a reader returning the attribute for which the backend is defined (#attribute)

  • a reader returning options configuring the backend (#options)

  • a constructor setting these three elements (model, attribute, options), and extracting fallbacks from the options hash (#initialize)

  • a setup method adding any configuration code to the model class (Setup#setup)

On top of this, a backend will normally:

  • implement a read instance method to read from the backend

  • implement a write instance method to write to the backend

  • implement a configure! class method to apply any normalization to the options hash

  • call the setup method yielding attributes and options to configure the model class

Examples:

Defining a Backend

class MyBackend
  include Backend

  def read(locale, **options)
    # ...
  end

  def write(locale, value, **options)
    # ...
  end

  def self.configure!(options)
    # ...
  end

  setup do |attributes, options|
    # Do something with attributes and options in context of model class.
  end
end

See Also:

Defined Under Namespace

Modules: ActiveModel, ActiveRecord, Cache, Column, Dirty, Fallbacks, Hstore, Jsonb, KeyValue, OrmDelegator, Sequel, Serialized, Setup, Table Classes: Null

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributeString (readonly)

Returns Backend attribute.

Returns:

  • (String)

    Backend attribute



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

def attribute
  @attribute
end

#modelObject (readonly)

Returns Model on which backend is defined.

Returns:

  • (Object)

    Model on which backend is defined



69
70
71
# File 'lib/mobility/backend.rb', line 69

def model
  @model
end

#optionsHash (readonly)

Returns Backend options.

Returns:

  • (Hash)

    Backend options



72
73
74
# File 'lib/mobility/backend.rb', line 72

def options
  @options
end

Class Method Details

.included(base) ⇒ Object

Extend included class with setup method



98
99
100
# File 'lib/mobility/backend.rb', line 98

def self.included(base)
  base.extend(Setup)
end

.method_name(attribute) ⇒ String

Returns name of backend reader method.

Parameters:

Returns:

  • (String)

    name of backend reader method



104
105
106
# File 'lib/mobility/backend.rb', line 104

def self.method_name(attribute)
  "#{attribute}_backend"
end

Instance Method Details

#initialize(model, attribute, **options) ⇒ Object

Parameters:

  • model

    Model on which backend is defined

  • attribute (String)

    Backend attribute

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • fallbacks (Hash)

    Fallbacks hash



78
79
80
81
82
83
84
# File 'lib/mobility/backend.rb', line 78

def initialize(model, attribute, **options)
  @model = model
  @attribute = attribute
  @options = options
  fallbacks = options[:fallbacks]
  @fallbacks = I18n::Locale::Fallbacks.new(fallbacks) if fallbacks.is_a?(Hash)
end