Module: Mobility
- Defined in:
- lib/mobility.rb,
lib/mobility/orm.rb,
lib/mobility/sequel.rb,
lib/mobility/backend.rb,
lib/mobility/version.rb,
lib/mobility/wrapper.rb,
lib/mobility/attributes.rb,
lib/mobility/translates.rb,
lib/mobility/active_model.rb,
lib/mobility/backend/null.rb,
lib/mobility/active_record.rb,
lib/mobility/backend/cache.rb,
lib/mobility/backend/dirty.rb,
lib/mobility/backend/jsonb.rb,
lib/mobility/backend/table.rb,
lib/mobility/configuration.rb,
lib/mobility/backend/column.rb,
lib/mobility/backend/hstore.rb,
lib/mobility/backend/sequel.rb,
lib/mobility/backend_resetter.rb,
lib/mobility/instance_methods.rb,
lib/mobility/backend/fallbacks.rb,
lib/mobility/backend/key_value.rb,
lib/mobility/backend/serialized.rb,
lib/mobility/sequel/translation.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/sequel/column_changes.rb,
lib/mobility/sequel/backend_resetter.rb,
lib/mobility/sequel/text_translation.rb,
lib/mobility/backend/sequel/key_value.rb,
lib/mobility/sequel/model_translation.rb,
lib/mobility/active_record/translation.rb,
lib/mobility/backend/sequel/serialized.rb,
lib/mobility/sequel/string_translation.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/generators/mobility/install_generator.rb,
lib/mobility/backend/active_record/column.rb,
lib/mobility/backend/active_record/hstore.rb,
lib/mobility/backend/sequel/query_methods.rb,
lib/mobility/active_model/backend_resetter.rb,
lib/mobility/active_model/attribute_methods.rb,
lib/mobility/active_record/backend_resetter.rb,
lib/mobility/active_record/text_translation.rb,
lib/mobility/active_record/model_translation.rb,
lib/mobility/backend/active_record/key_value.rb,
lib/mobility/active_record/string_translation.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
Mobility is a gem for storing and retrieving localized data through attributes on a class. The Mobility module includes all necessary methods and modules to support defining backend accessors on a class.
To enable Mobility on a class, simply include or extend the Mobility module, and define any attribute accessors using Translates#mobility_accessor (aliased to the value of Mobility.accessor_method, which defaults to translates
).
class MyClass
include Mobility
translates :title, backend: :key_value
end
When defining this module, Mobility attempts to require
various gems (for example, active_record
and sequel
) to evaluate which are loaded. Loaded gems are tracked with dynamic subclasses of the Loaded module and referenced in backends to define gem-dependent behavior.
Defined Under Namespace
Modules: ActiveModel, ActiveRecord, Backend, InstanceMethods, Loaded, Sequel, Translates Classes: Attributes, BackendRequired, BackendResetter, Configuration, InstallGenerator, InvalidLocale, VersionNotSupportedError, Wrapper
Constant Summary collapse
- VERSION =
"0.1.3"
Locale Accessors collapse
-
.locale ⇒ Symbol
Mobility locale.
-
.locale=(locale) ⇒ Symbol
Sets Mobility locale.
-
.with_locale(locale) {|Symbol| ... } ⇒ Object
Sets Mobility locale around block.
Configuration Methods collapse
-
.accessor_method ⇒ Symbol
Alias for mobility_accessor (defaults to
translates
). -
.config ⇒ Mobility::Configuration
Mobility configuration.
-
.configure {|Mobility::Configuration| ... } ⇒ Object
Configure Mobility.
-
.default_accessor_locales ⇒ Array<Symbol>
Default set of locales to use when defining accessors (defaults to
I18n.available_locales
). -
.default_backend ⇒ Symbol, Class
Default backend to use (can be symbol or actual backend class).
-
.default_fallbacks ⇒ I18n::Locale::Fallbacks
Default fallbacks instance.
Class Method Summary collapse
- .extended(model_class) ⇒ Object
-
.included(model_class) ⇒ Object
Extends model with this class so that include Mobility is equivalent to extend Mobility.
-
.normalize_locale(locale) ⇒ String
Return normalized locale.
-
.storage ⇒ RequestStore
Request store.
Class Method Details
.accessor_method ⇒ Symbol
Alias for mobility_accessor (defaults to translates
)
|
# File 'lib/mobility.rb', line 155
|
.config ⇒ Mobility::Configuration
Returns Mobility configuration.
151 152 153 |
# File 'lib/mobility.rb', line 151 def config storage[:mobility_configuration] ||= Mobility::Configuration.new end |
.configure {|Mobility::Configuration| ... } ⇒ Object
Configure Mobility
174 175 176 |
# File 'lib/mobility.rb', line 174 def configure yield config end |
.default_accessor_locales ⇒ Array<Symbol>
Default set of locales to use when defining accessors (defaults to I18n.available_locales
)
166 167 168 169 170 |
# File 'lib/mobility.rb', line 166 %w[accessor_method default_fallbacks default_backend default_accessor_locales].each do |method_name| define_method method_name do config.public_send(method_name) end end |
.default_backend ⇒ Symbol, Class
Default backend to use (can be symbol or actual backend class)
|
# File 'lib/mobility.rb', line 161
|
.default_fallbacks ⇒ I18n::Locale::Fallbacks
Default fallbacks instance
|
# File 'lib/mobility.rb', line 158
|
.extended(model_class) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/mobility.rb', line 79 def extended(model_class) return if model_class.respond_to? :mobility_accessor model_class.class_eval do def self.mobility @mobility ||= Mobility::Wrapper.new(self) end def self.translated_attribute_names mobility.translated_attribute_names end class << self include Translates if translates = Mobility.config.accessor_method alias_method translates, :mobility_accessor end end end model_class.include(InstanceMethods) if Loaded::ActiveRecord model_class.include(ActiveRecord) if model_class < ::ActiveRecord::Base model_class.include(ActiveModel::AttributeMethods) if model_class.ancestors.include?(::ActiveModel::AttributeMethods) end if Loaded::Sequel model_class.include(Sequel) if model_class < ::Sequel::Model end end |
.included(model_class) ⇒ Object
Extends model with this class so that include Mobility is equivalent to extend Mobility
112 113 114 |
# File 'lib/mobility.rb', line 112 def included(model_class) model_class.extend self end |
.locale ⇒ Symbol
Returns Mobility locale.
118 119 120 |
# File 'lib/mobility.rb', line 118 def locale read_locale || I18n.locale end |
.locale=(locale) ⇒ Symbol
Sets Mobility locale
126 127 128 |
# File 'lib/mobility.rb', line 126 def locale=(locale) set_locale(locale) end |
.normalize_locale(locale) ⇒ String
Return normalized locale
187 188 189 |
# File 'lib/mobility.rb', line 187 def normalize_locale(locale) "#{locale.to_s.downcase.sub("-", "_")}" end |
.storage ⇒ RequestStore
Returns Request store.
145 146 147 |
# File 'lib/mobility.rb', line 145 def storage RequestStore.store end |
.with_locale(locale) {|Symbol| ... } ⇒ Object
Sets Mobility locale around block
133 134 135 136 137 138 139 140 141 |
# File 'lib/mobility.rb', line 133 def with_locale(locale) previous_locale = read_locale begin set_locale(locale) yield(locale) ensure set_locale(previous_locale) end end |