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_'
Class Attribute Summary collapse
- .known ⇒ Object readonly
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
truethen 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
truethen 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 ⇒ void
This method resets all options to their default values.
Constructor Details
#initialize ⇒ InflectionOptions
This method initializes all internal structures.
273 274 275 |
# File 'lib/i18n-inflector/options.rb', line 273 def initialize reset end |
Class Attribute Details
.known ⇒ Object (readonly)
36 37 38 |
# File 'lib/i18n-inflector/options.rb', line 36 def known @known 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.
101 102 103 |
# File 'lib/i18n-inflector/options.rb', line 101 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.
76 77 78 |
# File 'lib/i18n-inflector/options.rb', line 76 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.
270 271 272 |
# File 'lib/i18n-inflector/options.rb', line 270 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.
138 139 140 |
# File 'lib/i18n-inflector/options.rb', line 138 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.
88 89 90 |
# File 'lib/i18n-inflector/options.rb', line 88 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.
117 118 119 |
# File 'lib/i18n-inflector/options.rb', line 117 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.
231 232 233 |
# File 'lib/i18n-inflector/options.rb', line 231 def unknown_defaults @unknown_defaults end |
Class Method Details
.attr_accessor(*args) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/i18n-inflector/options.rb', line 40 def attr_accessor(*args) r = old_attr_accessor(*args) @known ||= {} args.each do |arg| key = "@#{arg}" @known[key.to_sym] = (OPTION_PREFIX + arg.to_s).to_sym end r end |
.old_attr_accessor ⇒ Object
39 |
# File 'lib/i18n-inflector/options.rb', line 39 alias_method :old_attr_accessor, :attr_accessor |
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.
314 315 316 317 |
# File 'lib/i18n-inflector/options.rb', line 314 def clean_for_translate!() self.class.known.each_value { |long| .delete long } end |
#known ⇒ Array<Symbol>
Lists all known options in a long format (each name preceeded by inflector_).
324 325 326 |
# File 'lib/i18n-inflector/options.rb', line 324 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.
299 300 301 302 303 304 |
# File 'lib/i18n-inflector/options.rb', line 299 def () self.class.known .reject { |_name, long| .key?(long) } .each { |name, long| [long] = instance_variable_get(name) } end |
#reset ⇒ void
This method returns an undefined value.
This method resets all options to their default values.
280 281 282 283 284 285 286 287 288 289 |
# File 'lib/i18n-inflector/options.rb', line 280 def reset @unknown_defaults = true @traverses = true @interpolate_symbols = false @excluded_defaults = false @aliased_patterns = false @cache_aware = false @raises = false nil end |