Module: Mobility

Defined in:
lib/mobility.rb,
lib/mobility/util.rb,
lib/mobility/plugin.rb,
lib/mobility/backend.rb,
lib/mobility/plugins.rb,
lib/mobility/version.rb,
lib/mobility/backends.rb,
lib/mobility/pluggable.rb,
lib/mobility/plugins/arel.rb,
lib/mobility/translations.rb,
lib/mobility/backends/hash.rb,
lib/mobility/backends/json.rb,
lib/mobility/backends/null.rb,
lib/mobility/plugins/cache.rb,
lib/mobility/plugins/dirty.rb,
lib/mobility/plugins/query.rb,
lib/mobility/backends/jsonb.rb,
lib/mobility/backends/table.rb,
lib/mobility/plugins/reader.rb,
lib/mobility/plugins/sequel.rb,
lib/mobility/plugins/writer.rb,
lib/mobility/backends/column.rb,
lib/mobility/backends/hstore.rb,
lib/mobility/backends/sequel.rb,
lib/mobility/plugins/backend.rb,
lib/mobility/plugins/default.rb,
lib/mobility/plugins/presence.rb,
lib/mobility/plugins/fallbacks.rb,
lib/mobility/backends/container.rb,
lib/mobility/backends/key_value.rb,
lib/mobility/plugins/arel/nodes.rb,
lib/mobility/plugins/attributes.rb,
lib/mobility/backends/serialized.rb,
lib/mobility/backends/hash_valued.rb,
lib/mobility/backends/sequel/json.rb,
lib/mobility/plugins/active_model.rb,
lib/mobility/plugins/sequel/cache.rb,
lib/mobility/plugins/sequel/dirty.rb,
lib/mobility/plugins/sequel/query.rb,
lib/mobility/backends/sequel/jsonb.rb,
lib/mobility/backends/sequel/table.rb,
lib/mobility/plugins/active_record.rb,
lib/mobility/backends/active_record.rb,
lib/mobility/backends/sequel/column.rb,
lib/mobility/backends/sequel/hstore.rb,
lib/mobility/plugins/backend_reader.rb,
lib/mobility/plugins/sequel/backend.rb,
lib/mobility/backends/sequel/pg_hash.rb,
lib/mobility/plugins/column_fallback.rb,
lib/mobility/plugins/locale_accessors.rb,
lib/mobility/backends/sequel/container.rb,
lib/mobility/backends/sequel/key_value.rb,
lib/mobility/plugins/arel/nodes/pg_ops.rb,
lib/mobility/plugins/attribute_methods.rb,
lib/mobility/backends/sequel/serialized.rb,
lib/mobility/plugins/active_model/cache.rb,
lib/mobility/plugins/active_model/dirty.rb,
lib/mobility/backends/active_record/json.rb,
lib/mobility/plugins/active_record/cache.rb,
lib/mobility/plugins/active_record/dirty.rb,
lib/mobility/plugins/active_record/query.rb,
lib/mobility/backends/active_record/jsonb.rb,
lib/mobility/backends/active_record/table.rb,
lib/mobility/backends/active_record/column.rb,
lib/mobility/backends/active_record/hstore.rb,
lib/mobility/plugins/active_record/backend.rb,
lib/mobility/plugins/fallthrough_accessors.rb,
lib/mobility/backends/active_record/pg_hash.rb,
lib/mobility/plugins/sequel/column_fallback.rb,
lib/mobility/backends/active_record/container.rb,
lib/mobility/backends/active_record/key_value.rb,
lib/mobility/backends/active_record/serialized.rb,
lib/rails/generators/mobility/install_generator.rb,
lib/mobility/plugins/active_record/column_fallback.rb,
lib/rails/generators/mobility/translations_generator.rb,
lib/rails/generators/mobility/backend_generators/base.rb,
lib/mobility/plugins/active_record/uniqueness_validation.rb,
lib/rails/generators/mobility/backend_generators/table_backend.rb,
lib/rails/generators/mobility/backend_generators/column_backend.rb,
lib/rails/generators/mobility/active_record_migration_compatibility.rb

Defined Under Namespace

Modules: ActiveRecordMigrationCompatibility, Backend, BackendGenerators, Backends, Plugin, Plugins, Util, VERSION Classes: Error, InstallGenerator, InvalidLocale, NotImplementedError, Pluggable, Translations, TranslationsGenerator

Locale Accessors collapse

Class Method Summary collapse

Class Method Details

.available_localesArray<Symbol>

Note:

The special case for Rails is necessary due to the fact that Rails may load the model before setting I18n.available_locales. If we simply default to I18n.available_locales, we may define many more methods (in LocaleAccessors) than is really necessary.

Returns available locales. Defaults to I18n.available_locales, but will use Rails.application.config.i18n.available_locales if Rails is loaded and config is non-nil.

Returns:

  • (Array<Symbol>)

    Available locales


229
230
231
232
233
234
235
# File 'lib/mobility.rb', line 229

def available_locales
  if defined?(Rails) && Rails.respond_to?(:application) && Rails.application
    Rails.application.config.i18n.available_locales&.map(&:to_sym) || I18n.available_locales
  else
    I18n.available_locales
  end
end

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

Configure Mobility


117
118
119
120
121
122
123
124
# File 'lib/mobility.rb', line 117

def configure(&block)
  translates_with(Class.new(Translations)) unless @translations_class
  if block.arity == 0
    translations_class.instance_exec(&block)
  else
    yield translations_class
  end
end

.default_backendSymbol, Class

Alias to default backend defined on *translations_class+.

Returns:

  • (Symbol, Class)

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

def default_backend
  translations_class.defaults[:backend]&.first
end

.enforce_available_locales!(locale) ⇒ Object

Raises InvalidLocale exception if the locale passed in is present but not available.

Parameters:

  • locale (String, Symbol)

Raises:


217
218
219
# File 'lib/mobility.rb', line 217

def enforce_available_locales!(locale)
  raise Mobility::InvalidLocale.new(locale) unless (locale.nil? || available_locales.include?(locale.to_sym))
end

.extended(model_class) ⇒ Object


96
97
98
99
100
# File 'lib/mobility.rb', line 96

def extended(model_class)
  def model_class.translates(*args, **options)
    include Mobility.translations_class.new(*args, **options)
  end
end

.gem_versionObject


4
5
6
# File 'lib/mobility/version.rb', line 4

def self.gem_version
  Gem::Version.new VERSION::STRING
end

.included(model_class) ⇒ Object

Extends model with this class so that include Mobility is equivalent to extend Mobility (but extend is preferred).

Parameters:

  • model_class

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

def included(model_class)
  model_class.extend self
end

.localeSymbol

Returns Mobility locale.

Returns:

  • (Symbol)

    Mobility locale


143
144
145
# File 'lib/mobility.rb', line 143

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 Mobility.available_locales (if I18n.enforce_available_locales is true)


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

def locale=(locale)
  set_locale(locale)
end

.normalize_locale(locale = Mobility.locale) ⇒ String Also known as: normalized_locale

Return normalized locale

Examples:

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

Parameters:

  • locale (String, Symbol) (defaults to: Mobility.locale)

Returns:

  • (String)

    Normalized locale


183
184
185
# File 'lib/mobility.rb', line 183

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

.normalize_locale_accessor(attribute, locale = Mobility.locale) ⇒ String

Return normalized locale accessor name

Examples:

Mobility.normalize_locale_accessor(:foo, :ja)
#=> "foo_ja"
Mobility.normalize_locale_accessor(:bar, "pt-BR")
#=> "bar_pt_br"

Parameters:

  • attribute (String, Symbol)
  • locale (String, Symbol) (defaults to: Mobility.locale)

Returns:

  • (String)

    Normalized locale accessor name

Raises:

  • (ArgumentError)

    if generated accessor has an invalid format


198
199
200
201
202
203
204
# File 'lib/mobility.rb', line 198

def normalize_locale_accessor(attribute, locale = Mobility.locale)
  "#{attribute}_#{normalize_locale(locale)}".tap do |accessor|
    unless CALL_COMPILABLE_REGEXP.match(accessor)
      raise ArgumentError, "#{accessor.inspect} is not a valid accessor"
    end
  end
end

.reset_translations_classObject


137
138
139
# File 'lib/mobility.rb', line 137

def reset_translations_class
  @translations_class = nil
end

.storageRequestStore

Returns Request store.

Returns:

  • (RequestStore)

    Request store


171
172
173
# File 'lib/mobility.rb', line 171

def storage
  RequestStore.store
end

.translates_with(pluggable) ⇒ Object

Raises:

  • (ArgumentError)

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

def translates_with(pluggable)
  raise ArgumentError, "translations class must be a subclass of Module." unless Module === pluggable
  @translations_class = pluggable
end

.translations_classObject


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

def translations_class
  @translations_class ||
    raise(Error, "Mobility has not been configured. "\
          "Configure with Mobility.configure, or assign a translations class with Mobility.translates_with(<class>)")
end

.validate_locale!(locale) ⇒ Object

Check that a non-nil locale is valid. (Does not actually parse locale to check its format.)

Raises:


209
210
211
212
# File 'lib/mobility.rb', line 209

def validate_locale!(locale)
  raise Mobility::InvalidLocale.new(locale) unless Symbol === locale
  enforce_available_locales!(locale) if I18n.enforce_available_locales
end

.with_locale(locale) {|Symbol| ... } ⇒ Object

Sets Mobility locale around block

Parameters:

  • locale (Symbol)

    Locale to set in block

Yields:

  • (Symbol)

    Locale


159
160
161
162
163
164
165
166
167
# File 'lib/mobility.rb', line 159

def with_locale(locale)
  previous_locale = read_locale
  begin
    set_locale(locale)
    yield(locale)
  ensure
    set_locale(previous_locale)
  end
end