Module: Mobility::Backend

Includes:
Enumerable
Included in:
Mobility::Backends::Null
Defined in:
lib/mobility/backend.rb,
lib/mobility/backend/orm_delegator.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 constructor setting these two elements (model, attribute), 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 an each_locale instance method to iterate through available locales (used to define other Enumerable traversal and search methods)

  • 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 Mobility::Backend

  def read(locale, options = {})
    # ...
  end

  def write(locale, value, options = {})
    # ...
  end

  def each_locale
    # ...
  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: ClassMethods, OrmDelegator Classes: Translation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributeString (readonly)


61
62
63
# File 'lib/mobility/backend.rb', line 61

def attribute
  @attribute
end

#modelObject (readonly)


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

def model
  @model
end

Class Method Details

.included(base) ⇒ Object

Extend included class with setup method and other class methods


119
120
121
122
123
124
125
# File 'lib/mobility/backend.rb', line 119

def self.included(base)
  base.extend ClassMethods
  def base.options
    @options
  end
  base.option_reader :model_class
end

.method_name(attribute) ⇒ String


129
130
131
132
# File 'lib/mobility/backend.rb', line 129

def self.method_name(attribute)
  @backend_method_names ||= {}
  @backend_method_names[attribute] ||= "#{attribute}_backend"
end

Instance Method Details

#each {|Translation| ... } ⇒ Object

Yields translations to block

Yield Parameters:


93
94
95
# File 'lib/mobility/backend.rb', line 93

def each
  each_locale { |locale| yield Translation.new(self, locale) }
end

#each_locale {|Locale| ... } ⇒ Object

Yields locales available for this attribute.

Yield Parameters:

  • Locale (Symbol)

88
89
# File 'lib/mobility/backend.rb', line 88

def each_locale
end

#initialize(model, attribute) ⇒ Object


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

def initialize(model, attribute)
  @model = model
  @attribute = attribute
end

#localesArray<Symbol>

List locales available for this backend.


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

def locales
  map(&:locale)
end

#model_classClass

Returns name of model in which backend is used.


# File 'lib/mobility/backend.rb', line 109

#optionsHash


114
115
116
# File 'lib/mobility/backend.rb', line 114

def options
  self.class.options
end

#present?(locale, options = {}) ⇒ TrueClass, FalseClass


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

def present?(locale, options = {})
  Util.present?(read(locale, options))
end