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

Configuration Methods collapse

Class Method Summary collapse

Class Method Details

.accessor_methodSymbol

Alias for mobility_accessor (defaults to translates)

Returns:

  • (Symbol)


# File 'lib/mobility.rb', line 155

.configMobility::Configuration

Returns Mobility configuration.

Returns:



151
152
153
# File 'lib/mobility.rb', line 151

def config
  storage[:mobility_configuration] ||= Mobility::Configuration.new
end

.configure {|Mobility::Configuration| ... } ⇒ Object

Configure Mobility

Yields:



174
175
176
# File 'lib/mobility.rb', line 174

def configure
  yield config
end

.default_accessor_localesArray<Symbol>

Default set of locales to use when defining accessors (defaults to I18n.available_locales)

Returns:

  • (Array<Symbol>)


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_backendSymbol, Class

Default backend to use (can be symbol or actual backend class)

Returns:

  • (Symbol, Class)


# File 'lib/mobility.rb', line 161

.default_fallbacksI18n::Locale::Fallbacks

Default fallbacks instance

Returns:

  • (I18n::Locale::Fallbacks)


# 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

Parameters:

  • model_class


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

def included(model_class)
  model_class.extend self
end

.localeSymbol

Returns Mobility locale.

Returns:

  • (Symbol)

    Mobility locale



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

def locale
  read_locale || I18n.locale
end

.locale=(locale) ⇒ Symbol

Sets Mobility locale

Parameters:

  • locale (Symbol)

    Locale to set

Returns:

  • (Symbol)

    Locale

Raises:

  • (InvalidLocale)

    if locale is nil or not in +I18n.available_locales



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

def locale=(locale)
  set_locale(locale)
end

.normalize_locale(locale) ⇒ String

Return normalized locale

Examples:

Mobility.normalize_locale(:ja)
#=> "ja"
Mobility.normalize_locale("pt-BR")
#=> "pt_br"

Parameters:

Returns:

  • (String)

    Normalized locale



187
188
189
# File 'lib/mobility.rb', line 187

def normalize_locale(locale)
  "#{locale.to_s.downcase.sub("-", "_")}"
end

.storageRequestStore

Returns Request store.

Returns:

  • (RequestStore)

    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

Parameters:

  • locale (Symbol)

    Locale to set in block

Yields:

  • (Symbol)

    Locale



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