module Mobility::Backend
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 otherEnumerable
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 invalid_keys
-
call the
setup
method yielding attributes and options to configure the model class
@example 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
Constants
- Translation
Attributes
@return [String] Backend
attribute
@return [Object] Model on which backend is defined
Public Class Methods
Extend included class with setup
method and other class methods
# File lib/mobility/backend.rb, line 118 def self.included(base) base.extend ClassMethods base.singleton_class.attr_reader :options, :model_class end
@!macro [new] backend_constructor
@param model Model on which backend is defined @param [String] attribute Backend attribute
# File lib/mobility/backend.rb, line 68 def initialize(*args) @model = args[0] @attribute = args[1] end
Public Instance Methods
Yields translations to block @yieldparam [Mobility::Backend::Translation] Translation
# File lib/mobility/backend.rb, line 92 def each each_locale { |locale| yield Translation.new(self, locale) } end
@!macro [new] backend_iterator
Yields locales available for this attribute. @yieldparam [Symbol] Locale
# File lib/mobility/backend.rb, line 87 def each_locale end
List locales available for this backend. @return [Array<Symbol>] Array of available locales
# File lib/mobility/backend.rb, line 98 def locales map(&:locale) end
@return [Hash] options
# File lib/mobility/backend.rb, line 113 def options self.class.options end
@param [Symbol] locale Locale to read @return [TrueClass,FalseClass] Whether translation is present for locale
# File lib/mobility/backend.rb, line 104 def present?(locale, options = {}) Util.present?(read(locale, **options)) end