Module: Mobility::Backend

Includes:
Enumerable
Included in:
Mobility::Backends::Hash, Mobility::Backends::Null
Defined in:
lib/mobility/backend.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)

  • a setup method adding any configuration code to the model class (ClassMethods#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 valid_keys class method returning an array of symbols corresponding to valid keys for configuring this backend.

  • implement a configure class method to apply any normalization to the keys on the options hash included in valid_keys

  • call the setup method yielding attributes and options (and optionally the configured backend class) 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

  # The block can optionally take the configured backend class as its third
  # argument:
  #
  # setup do |attributes, options, backend_class|
  #   ...
  # end
end

See Also:

Defined Under Namespace

Modules: ClassMethods, ConfiguredBackend Classes: ConfiguredError, Translation, UnconfiguredError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributeString (readonly)

Returns Backend attribute.

Returns:

  • (String)

    Backend attribute



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

def attribute
  @attribute
end

#modelObject (readonly)

Returns Model on which backend is defined.

Returns:

  • (Object)

    Model on which backend is defined



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

def model
  @model
end

Class Method Details

.included(base) ⇒ Object

Extend included class with setup method and other class methods



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

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

Instance Method Details

#==(backend) ⇒ Object



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

def ==(backend)
  backend.class == self.class &&
    backend.attribute == attribute &&
    backend.model == model
end

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

Yields translations to block

Yield Parameters:



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

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)


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

def each_locale
end

#initialize(*args) ⇒ Object

Parameters:

  • model

    Model on which backend is defined

  • attribute (String)

    Backend attribute



75
76
77
78
# File 'lib/mobility/backend.rb', line 75

def initialize(*args)
  @model = args[0]
  @attribute = args[1]
end

#localesArray<Symbol>

List locales available for this backend.

Returns:

  • (Array<Symbol>)

    Array of available locales



111
112
113
# File 'lib/mobility/backend.rb', line 111

def locales
  map(&:locale)
end

#model_classClass

Returns name of model in which backend is used.

Returns:

  • (Class)

    Model class



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

#optionsHash

Returns options.

Returns:

  • (Hash)

    options



126
127
128
# File 'lib/mobility/backend.rb', line 126

def options
  self.class.options
end

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

Returns Whether translation is present for locale.

Parameters:

  • locale (Symbol)

    Locale to read

Returns:

  • (TrueClass, FalseClass)

    Whether translation is present for locale



117
118
119
# File 'lib/mobility/backend.rb', line 117

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