Class: I18n::Inflector::InflectionOptions
- Inherits:
-
Object
- Object
- I18n::Inflector::InflectionOptions
- Defined in:
- lib/i18n-inflector/options.rb
Overview
This class uses modified version of attr_accessor
that memorizes any added method name as an option name. These options (with inflector_
prefix added) are accessible through #known method. The last method is used by options preparing routine when the interpolation is performed.
This class contains structures for keeping parsed translation data and basic operations.
All global options are available for current backend’s inflector by calling:
I18n.backend.inflector.options.<option_name>
or just:
I18n.inflector.options.<option_name>
A global option may be overriden by passing a proper option to the translation method. Such option should have the name of a global option but prefixed with inflector_
:
translate('welcome', :inflector_<option_name> => value)
Constant Summary collapse
- OPTION_PREFIX =
Prefix used to mark option as a controlling option.
'inflector_'
Instance Attribute Summary collapse
-
#aliased_patterns ⇒ Boolean
This is a switch that enables you to use aliases in patterns.
-
#cache_aware ⇒ Boolean
This switch enables cache-aware mode.
-
#excluded_defaults ⇒ Boolean
When this switch is set to
true
then inflector falls back to the default token for a kind if the given inflection option is correct but doesn’t exist in a pattern. -
#interpolate_symbols ⇒ Boolean
This is a switch that enables interpolation of symbols.
-
#raises ⇒ Boolean
This is a switch that enables extended error reporting.
-
#traverses ⇒ Boolean
This is a switch that enables you to interpolate patterns contained in resulting nested Hashes.
-
#unknown_defaults ⇒ Boolean
When this switch is set to
true
then inflector falls back to the default token for a kind if an inflection option passed to the Backend::Inflector#translate is unknown ornil
.
Class Method Summary collapse
Instance Method Summary collapse
-
#clean_for_translate!(options) ⇒ Hash
This method prepares options for translate method.
-
#initialize ⇒ InflectionOptions
constructor
This method initializes all internal structures.
-
#known ⇒ Array<Symbol>
Lists all known options in a long format (each name preceeded by
inflector_
). -
#prepare_options!(options) ⇒ Hash
This method processes the given argument in a way that it will use default values for options that are missing.
-
#reset
This method resets all options to their default values.
Constructor Details
#initialize ⇒ InflectionOptions
This method initializes all internal structures.
278 279 280 |
# File 'lib/i18n-inflector/options.rb', line 278 def initialize reset end |
Instance Attribute Details
#aliased_patterns ⇒ Boolean
Local option :inflector_aliased_patterns
passed to the Backend::Inflector#translate overrides this setting.
This is a switch that enables you to use aliases in patterns. When it’s enabled then aliases may be used in inflection patterns, not only true tokens. This operation may make your translation data a bit messy if you’re not alert. That’s why this switch is by default set to false
.
106 107 108 |
# File 'lib/i18n-inflector/options.rb', line 106 def aliased_patterns @aliased_patterns end |
#cache_aware ⇒ Boolean
This switch enables cache-aware mode. In that mode inflection options and flags are evaluated before calling original translate method and all options are passed to that method. Because options preparation for inflection methods is explicit (any missing switches and their default values are added to options) then original translate (or proxy caching method) will receive even those options that might have been changed globally.
Caching modules for I18n may use options passed to the translate method (if they are plugged in before inflector) for key transformation since the inflection options may influence the interpolation process and therefore the resulting string.
If however, the caching variant of the translate method is positioned before inflected variant in methods chain, then the only way of knowing all the settings by caching routine is to call options.options.prepare_options!(options)
on the used backend, for example:
I18n.backend.inflector..prepare()
That will alter the options
data so they will contain all switches and values.
81 82 83 |
# File 'lib/i18n-inflector/options.rb', line 81 def cache_aware @cache_aware end |
#excluded_defaults ⇒ Boolean
Local option :inflector_excluded_defaults
passed to the Backend::Inflector#translate overrides this setting.
When this switch is set to true
then inflector falls back to the default token for a kind if the given inflection option is correct but doesn’t exist in a pattern.
There might happen that the inflection option given to #translate method will contain some proper token, but that token will not be present in a processed pattern. Normally an empty string will be generated from such a pattern or a free text (if a local fallback is present in a pattern). You can change that behavior and tell interpolating routine to use the default token for a processed kind in such cases.
This switch is by default set to false
.
275 276 277 |
# File 'lib/i18n-inflector/options.rb', line 275 def excluded_defaults @excluded_defaults end |
#interpolate_symbols ⇒ Boolean
Local option :inflector_interpolate_symbols
passed to the Backend::Inflector#translate overrides this setting.
This is a switch that enables interpolation of symbols. Whenever interpolation method will receive a collection of symbols as a result of calling underlying translation method it won’t process them, returning as they are, unless this switch is enabled.
Note that using symbols as values in translation data creates I18n aliases. This option is intended to work with arrays of symbols or hashes with symbols as values, if the original translation method returns such structures.
This switch is by default set to false
.
143 144 145 |
# File 'lib/i18n-inflector/options.rb', line 143 def interpolate_symbols @interpolate_symbols end |
#raises ⇒ Boolean
Local option :inflector_raises
passed to the Backend::Inflector#translate overrides this setting.
This is a switch that enables extended error reporting. When it’s enabled then errors are raised in case of unknown or empty tokens present in a pattern or in options. This switch is by default set to false
.
93 94 95 |
# File 'lib/i18n-inflector/options.rb', line 93 def raises @raises end |
#traverses ⇒ Boolean
Local option :inflector_traverses
passed to the Backend::Inflector#translate overrides this setting.
This is a switch that enables you to interpolate patterns contained in resulting nested Hashes. It is used when the original translation method returns a subtree of translation data because the given key is not pointing to a leaf of the data but to some collection.
This switch is by default set to true
. When you turn it off then the Inflector won’t touch nested results and will return them as they are.
122 123 124 |
# File 'lib/i18n-inflector/options.rb', line 122 def traverses @traverses end |
#unknown_defaults ⇒ Boolean
Local option :inflector_unknown_defaults
passed to the Backend::Inflector#translate overrides this setting.
When this switch is set to true
then inflector falls back to the default token for a kind if an inflection option passed to the Backend::Inflector#translate is unknown or nil
. Note that the value of the default token will be interpolated only when this token is present in a pattern. This switch is by default set to true
.
236 237 238 |
# File 'lib/i18n-inflector/options.rb', line 236 def unknown_defaults @unknown_defaults end |
Class Method Details
.attr_accessor(*args) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/i18n-inflector/options.rb', line 44 def attr_accessor(*args) r = old_attr_accessor(*args) @known ||= Hash.new args.each do |arg| key = '@' << arg.to_s @known[key.to_sym] = ("" << OPTION_PREFIX << arg.to_s).to_sym end r end |
Instance Method Details
#clean_for_translate!(options) ⇒ Hash
It modifies the given object.
This method prepares options for translate method. That means removal of all kind-related options and all options that are flags.
319 320 321 322 |
# File 'lib/i18n-inflector/options.rb', line 319 def clean_for_translate!() self.class.known.each { |name,long| .delete long } end |
#known ⇒ Array<Symbol>
Lists all known options in a long format (each name preceeded by inflector_
).
329 330 331 |
# File 'lib/i18n-inflector/options.rb', line 329 def known self.class.known.values end |
#prepare_options!(options) ⇒ Hash
It modifies the given object.
This method processes the given argument in a way that it will use default values for options that are missing.
304 305 306 307 308 309 |
# File 'lib/i18n-inflector/options.rb', line 304 def () self.class.known. reject { |name,long| .has_key?(long) }. each { |name,long| [long] = instance_variable_get(name) } end |
#reset
This method returns an undefined value.
This method resets all options to their default values.
285 286 287 288 289 290 291 292 293 294 |
# File 'lib/i18n-inflector/options.rb', line 285 def reset @unknown_defaults = true @traverses = true @interpolate_symbols= false @excluded_defaults = false @aliased_patterns = false @cache_aware = false @raises = false nil end |