Module: Mobility::Backend
- Includes:
- Enumerable
- Included in:
- Mobility::Backends::Null
- Defined in:
- lib/mobility/backend.rb,
lib/mobility/backend/orm_delegator.rb,
lib/mobility/backend/stringify_locale.rb
Overview
Defines a minimum set of shared components included in any backend. These are:
- a reader returning the
modelon which the backend is defined (#model) - a reader returning the
attributefor which the backend is defined (#attribute) - a constructor setting these two elements (+model+,
attribute), and extracting fallbacks from the options hash (#initialize) - a
setupmethod adding any configuration code to the model class (Setup#setup)
On top of this, a backend will normally:
- implement a
readinstance method to read from the backend - implement a
writeinstance method to write to the backend - implement an
each_localeinstance method to iterate through available locales (used to define otherEnumerabletraversal and search methods) - implement a
configureclass method to apply any normalization to the options hash - call the
setupmethod yielding attributes and options to configure the model class
Defined Under Namespace
Modules: ClassMethods, OrmDelegator, Setup, StringifyLocale Classes: Translation
Instance Attribute Summary collapse
-
#attribute ⇒ String
readonly
Backend attribute.
-
#model ⇒ Object
readonly
Model on which backend is defined.
Class Method Summary collapse
-
.included(base) ⇒ Object
Extend included class with
setupmethod. -
.method_name(attribute) ⇒ String
Name of backend reader method.
Instance Method Summary collapse
-
#each {|Translation| ... } ⇒ Object
Yields translations to block.
-
#each_locale {|Locale| ... } ⇒ Object
Yields locales available for this attribute.
- #initialize(model, attribute, **_) ⇒ Object
-
#locales ⇒ Array<Symbol>
List locales available for this backend.
-
#present?(locale, options = {}) ⇒ TrueClass, FalseClass
Whether translation is present for locale.
Instance Attribute Details
#attribute ⇒ String (readonly)
Returns Backend attribute.
61 62 63 |
# File 'lib/mobility/backend.rb', line 61 def attribute @attribute end |
#model ⇒ Object (readonly)
Returns Model on which backend is defined.
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
110 111 112 113 |
# File 'lib/mobility/backend.rb', line 110 def self.included(base) base.extend(Setup) base.extend(ClassMethods) end |
.method_name(attribute) ⇒ String
Returns name of backend reader method.
117 118 119 120 |
# File 'lib/mobility/backend.rb', line 117 def self.method_name(attribute) @backend_method_names ||= {} @backend_method_names[attribute] ||= "#{attribute}_backend".freeze end |
Instance Method Details
#each {|Translation| ... } ⇒ Object
Yields translations to block
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.
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 |
#locales ⇒ Array<Symbol>
List locales available for this backend.
99 100 101 |
# File 'lib/mobility/backend.rb', line 99 def locales map(&:locale) end |