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
-
.locale ⇒ Symbol
Mobility locale.
-
.locale=(locale) ⇒ Symbol
Sets Mobility locale.
-
.with_locale(locale) {|Symbol| ... } ⇒ Object
Sets Mobility locale around block.
Class Method Summary collapse
-
.available_locales ⇒ Array<Symbol>
Returns available locales.
-
.configure {|Mobility::Translations| ... } ⇒ Object
Configure Mobility.
-
.default_backend ⇒ Symbol, Class
Alias to default backend defined on *translations_class+.
-
.enforce_available_locales!(locale) ⇒ Object
Raises InvalidLocale exception if the locale passed in is present but not available.
- .extended(model_class) ⇒ Object
- .gem_version ⇒ Object
-
.included(model_class) ⇒ Object
Extends model with this class so that include Mobility is equivalent to extend Mobility (but
extend
is preferred). -
.normalize_locale(locale = Mobility.locale) ⇒ String
(also: normalized_locale)
Return normalized locale.
-
.normalize_locale_accessor(attribute, locale = Mobility.locale) ⇒ String
Return normalized locale accessor name.
- .reset_translations_class ⇒ Object
-
.storage ⇒ RequestStore
Request store.
- .translates_with(pluggable) ⇒ Object
- .translations_class ⇒ Object
-
.validate_locale!(locale) ⇒ Object
Check that a non-nil locale is valid.
Class Method Details
.available_locales ⇒ Array<Symbol>
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.
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_backend ⇒ Symbol, Class
Alias to default backend defined on *translations_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.
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, **) include Mobility.translations_class.new(*args, **) end end |
.gem_version ⇒ Object
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).
105 106 107 |
# File 'lib/mobility.rb', line 105 def included(model_class) model_class.extend self end |
.locale ⇒ Symbol
Returns Mobility locale.
143 144 145 |
# File 'lib/mobility.rb', line 143 def locale read_locale || I18n.locale end |
.locale=(locale) ⇒ Symbol
Sets Mobility locale
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
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
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_class ⇒ Object
137 138 139 |
# File 'lib/mobility.rb', line 137 def reset_translations_class @translations_class = nil end |
.storage ⇒ RequestStore
Returns Request store.
171 172 173 |
# File 'lib/mobility.rb', line 171 def storage RequestStore.store end |
.translates_with(pluggable) ⇒ Object
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_class ⇒ Object
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.)
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
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 |