Class: I18n::Inflector::API_Strict
- Inherits:
-
Object
- Object
- I18n::Inflector::API_Strict
- Defined in:
- lib/i18n-inflector/api_strict.rb
Overview
This class contains common operations that can be performed on inflection data describing strict kinds and tokens assigned to them (used in named patterns). It is used by the regular API and present there as strict instance attribute.
It operates on the database containing instances of InflectionData_Strict indexed by locale names and has methods to access the inflection data in an easy way. It can operate on a database and options passed to initializer; if they aren’t passet it will create them.
Usage
You can access the instance of this class attached to default I18n backend by calling:
I18n.backend.inflector.strict
or in a short form:
I18n.inflector.strict
In most cases using the regular API instance may be sufficient to operate on inflection data, because the regular API (instantiated as I18n.inflector
) is aware of strict kinds and can pass calls from API_Strict
object if the kind
argument given in a method call contains the @ symbol.
For an instance connected to default I18n backend the object containing inflection options is shared with the regular API.
Direct Known Subclasses
Instance Method Summary collapse
-
#add_database(db) ⇒ I18n::Inflector::InflectionData_Strict
Attaches InflectionData_Strict instance to the current object.
-
#aliases(kind = nil, locale = nil) ⇒ Hash
Gets inflection aliases belonging to a strict kind and their pointers.
-
#default_token(kind, locale = nil) ⇒ Symbol?
Reads default token of the given strict
kind
. -
#delete_database(locale)
Deletes a strict inflections database for the specified locale.
-
#each_alias(kind = nil, locale = nil) {|alias, target| ... } ⇒ LazyHashEnumerator
Iterates through inflection aliases belonging to a strict kind and their pointers.
-
#each_inflected_locale(kind = nil) {|locale| ... } ⇒ LazyArrayEnumerator
(also: #each_locale, #each_supported_locale)
Iterates through locales which have configured strict inflection support.
-
#each_kind(locale = nil) {|kind| ... } ⇒ LazyArrayEnumerator
(also: #each_inflection_kind)
Iterates through known strict inflection kinds.
-
#each_token(kind = nil, locale = nil) {|token, description| ... } ⇒ LazyHashEnumerator
Iterates through available inflection tokens belonging to a strict kind and their descriptions.
-
#each_token_raw(kind = nil, locale = nil) {|token, value| ... } ⇒ LazyHashEnumerator
(also: #each_raw_token)
Iterates through available inflection tokens belonging to a strict kind and their values.
-
#each_token_true(kind = nil, locale = nil) {|token, description| ... } ⇒ LazyHashEnumerator
(also: #each_true_token)
Iterates through inflection tokens belonging to a strict kind and their values.
-
#has_alias?(*args) ⇒ Boolean
(also: #token_is_alias?)
Checks if the given
token
belonging to a strict kind is an alias. -
#has_kind?(kind, locale = nil) ⇒ Boolean
Tests if a strict kind exists.
-
#has_token?(*args) ⇒ Boolean
(also: #token_exists?)
Checks if the given
token
belonging to a strict kind exists. -
#has_true_token?(*args) ⇒ Boolean
(also: #token_is_true?)
Checks if the given
token
belonging to a strict kind is a true token (not alias). -
#inflected_locale?(locale = nil) ⇒ Boolean
(also: #locale?, #locale_supported?)
Checks if the given locale was configured to support strict inflection.
-
#inflected_locales(kind = nil) ⇒ Array<Symbol>
(also: #locales, #supported_locales)
Gets locales which have configured strict inflection support.
-
#initialize(idb = nil, options = nil) ⇒ API_Strict
constructor
Initilizes inflector by connecting to internal databases used for storing inflection data and options.
-
#kind(token, kind = nil, locale = nil) ⇒ Symbol?
Gets a kind of the given
token
(which may be an alias) belonging to a strict kind. -
#kinds(locale = nil) ⇒ Array<Symbol>
(also: #inflection_kinds)
Gets known strict inflection kinds.
-
#new_database(locale) ⇒ I18n::Inflector::InflectionData_Strict
Creates an empty strict inflections database for the specified locale.
-
#prep_locale(locale = nil) ⇒ Symbol
protected
Processes
locale
identifier and validates whether it’s correct (not empty and notnil
). -
#tkl_args(args) ⇒ Array<Symbol,Symbol,Symbol>
protected
This method is the internal helper that prepares arguments containing
token
,kind
andlocale
. -
#token_description(*args) ⇒ String?
Gets the description of the given inflection token belonging to a strict kind.
-
#tokens(kind = nil, locale = nil) ⇒ Hash
Gets available inflection tokens belonging to a strict kind and their descriptions.
-
#tokens_raw(kind = nil, locale = nil) ⇒ Hash
(also: #raw_tokens)
Gets available inflection tokens belonging to a strict kind and their values.
-
#tokens_true(kind = nil, locale = nil) ⇒ Hash
(also: #true_tokens)
Gets true inflection tokens belonging to a strict kind and their values.
-
#true_token(*args) ⇒ Symbol?
(also: #resolve_alias)
Gets true token for the given
token
belonging to a strict kind.
Constructor Details
#initialize(idb = nil, options = nil) ⇒ API_Strict
If any given option is nil
then a proper object will be created. If it’s given, then it will be referenced, not copied.
Initilizes inflector by connecting to internal databases used for storing inflection data and options.
57 58 59 60 61 62 |
# File 'lib/i18n-inflector/api_strict.rb', line 57 def initialize(idb=nil, =nil) @idb = idb.nil? ? {} : idb @options = .nil? ? I18n::Inflector::InflectionOptions.new : @lazy_locales = LazyHashEnumerator.new(@idb) @inflected_locales_cache = Hash.new end |
Instance Method Details
#add_database(db) ⇒ I18n::Inflector::InflectionData_Strict
It doesn’t create copy of inflection database, it registers the given object.
Attaches InflectionData_Strict instance to the current object.
87 88 89 90 91 92 93 |
# File 'lib/i18n-inflector/api_strict.rb', line 87 def add_database(db) return nil if db.nil? locale = prep_locale(db.locale) delete_database(locale) @inflected_locales_cache.clear @idb[locale] = db end |
#aliases(kind) ⇒ Hash #aliases(kind, locale) ⇒ Hash
Gets inflection aliases belonging to a strict kind and their pointers.
564 565 566 |
# File 'lib/i18n-inflector/api_strict.rb', line 564 def aliases(kind=nil, locale=nil) each_alias(kind, locale).to_h end |
#default_token(kind) ⇒ Symbol? #default_token(kind, locale) ⇒ Symbol?
Reads default token of the given strict kind
.
242 243 244 245 |
# File 'lib/i18n-inflector/api_strict.rb', line 242 def default_token(kind, locale=nil) return nil if (kind.nil? || kind.to_s.empty?) data_safe(locale).get_default_token(kind.to_sym) end |
#delete_database(locale)
It detaches the database from I18n::Inflector::API_Strict instance. Other objects referring to it directly may still use it.
This method returns an undefined value.
Deletes a strict inflections database for the specified locale.
103 104 105 106 107 108 |
# File 'lib/i18n-inflector/api_strict.rb', line 103 def delete_database(locale) locale = prep_locale(locale) return nil if @idb[locale].nil? @inflected_locales_cache.clear @idb[locale] = nil end |
#each_alias(kind) ⇒ LazyHashEnumerator #each_alias(kind, locale) ⇒ LazyHashEnumerator
Iterates through inflection aliases belonging to a strict kind and their pointers.
545 546 547 548 |
# File 'lib/i18n-inflector/api_strict.rb', line 545 def each_alias(kind=nil, locale=nil, &block) kind = kind.to_s.empty? ? nil : kind.to_sym data_safe(locale).each_alias(kind, &block) end |
#each_inflected_locale ⇒ LazyArrayEnumerator #each_inflected_locale(kind) ⇒ LazyArrayEnumerator Also known as: each_locale, each_supported_locale
Iterates through locales which have configured strict inflection support.
142 143 144 145 146 147 |
# File 'lib/i18n-inflector/api_strict.rb', line 142 def each_inflected_locale(kind=nil, &block) kind = kind.to_s.empty? ? nil : kind.to_sym i = @lazy_locales.reject { |lang,data| data.empty? } i = i.select { |lang,data| data.has_kind?(kind) } unless kind.nil? i.each_key(&block) end |
#kinds ⇒ LazyArrayEnumerator #kinds(locale) ⇒ LazyArrayEnumerator Also known as: each_inflection_kind
Iterates through known strict inflection kinds.
185 186 187 |
# File 'lib/i18n-inflector/api_strict.rb', line 185 def each_kind(locale=nil, &block) data_safe(locale).each_kind(&block) end |
#each_token(kind) ⇒ LazyHashEnumerator #each_token(kind, locale) ⇒ LazyHashEnumerator
You cannot deduce where aliases are pointing to, since the information about a target is replaced by the description. To get targets use the #raw_tokens method. To simply list aliases and their targets use the #aliases method.
Iterates through available inflection tokens belonging to a strict kind and their descriptions.
394 395 396 397 |
# File 'lib/i18n-inflector/api_strict.rb', line 394 def each_token(kind=nil, locale=nil, &block) kind = kind.to_s.empty? ? nil : kind.to_sym data_safe(locale).each_token(kind, &block) end |
#each_token_raw(kind) ⇒ LazyHashEnumerator #each_token_raw(kind, locale) ⇒ LazyHashEnumerator Also known as: each_raw_token
You may deduce whether the returned values are aliases or true tokens by testing if a value is a kind of Symbol or a String.
Iterates through available inflection tokens belonging to a strict kind and their values.
446 447 448 449 |
# File 'lib/i18n-inflector/api_strict.rb', line 446 def each_token_raw(kind=nil, locale=nil, &block) kind = kind.to_s.empty? ? nil : kind.to_sym data_safe(locale).each_raw_token(kind, &block) end |
#each_token_true(kind) ⇒ LazyHashEnumerator #each_token_true(kind, locale) ⇒ LazyHashEnumerator Also known as: each_true_token
It returns only true tokens, not aliases.
Iterates through inflection tokens belonging to a strict kind and their values.
498 499 500 501 |
# File 'lib/i18n-inflector/api_strict.rb', line 498 def each_token_true(kind=nil, locale=nil, &block) kind = kind.to_s.empty? ? nil : kind.to_sym data_safe(locale).each_true_token(kind, &block) end |
#has_alias?(token, kind) ⇒ Boolean #has_alias?(token, kind, locale) ⇒ Boolean Also known as: token_is_alias?
Checks if the given token
belonging to a strict kind is an alias.
264 265 266 267 268 |
# File 'lib/i18n-inflector/api_strict.rb', line 264 def has_alias?(*args) token, kind, locale = tkl_args(args) return false if (token.nil? || kind.nil?) data_safe(locale).has_alias?(token, kind) end |
#has_kind?(kind) ⇒ Boolean #has_kind?(kind, locale) ⇒ Boolean
Tests if a strict kind exists.
221 222 223 224 |
# File 'lib/i18n-inflector/api_strict.rb', line 221 def has_kind?(kind, locale=nil) return false if (kind.nil? || kind.to_s.empty?) data_safe(locale).has_kind?(kind.to_sym) end |
#has_token?(token, kind) ⇒ Boolean #has_token?(token, kind, locale) ⇒ Boolean Also known as: token_exists?
Checks if the given token
belonging to a strict kind exists. It may be an alias or a true token.
312 313 314 315 316 |
# File 'lib/i18n-inflector/api_strict.rb', line 312 def has_token?(*args) token, kind, locale = tkl_args(args) return false if (token.nil? || kind.nil?) data_safe(locale).has_token?(token, kind) end |
#has_true_token?(token, kind) ⇒ Boolean #has_true_token?(token, kind, locale) ⇒ Boolean Also known as: token_is_true?
Checks if the given token
belonging to a strict kind is a true token (not alias).
288 289 290 291 292 |
# File 'lib/i18n-inflector/api_strict.rb', line 288 def has_true_token?(*args) token, kind, locale = tkl_args(args) return false if (token.nil? || kind.nil?) data_safe(locale).has_true_token?(token, kind) end |
#inflected_locale? ⇒ Boolean #inflected_locale?(locale) ⇒ Boolean Also known as: locale?, locale_supported?
Checks if the given locale was configured to support strict inflection.
122 123 124 |
# File 'lib/i18n-inflector/api_strict.rb', line 122 def inflected_locale?(locale=nil) not @idb[prep_locale(locale)].nil? rescue false end |
#inflected_locales ⇒ Array<Symbol> #inflected_locales(kind) ⇒ Array<Symbol> Also known as: locales, supported_locales
Gets locales which have configured strict inflection support.
162 163 164 165 166 |
# File 'lib/i18n-inflector/api_strict.rb', line 162 def inflected_locales(kind=nil) kind = kind.to_s.empty? ? nil : kind.to_sym r = ( @inflected_locales_cache[kind] ||= each_inflected_locale(kind).to_a ) r.nil? ? r : r.dup end |
#kind(token, kind) ⇒ Symbol? #kind(token, kind, locale) ⇒ Symbol?
Gets a kind of the given token
(which may be an alias) belonging to a strict kind.
365 366 367 368 |
# File 'lib/i18n-inflector/api_strict.rb', line 365 def kind(token, kind=nil, locale=nil) return nil if (token.nil? || kind.nil? || token.to_s.empty? || kind.to_s.empty?) data_safe(locale).get_kind(token.to_sym, kind.to_sym) end |
#kinds ⇒ Array<Symbol> #kinds(locale) ⇒ Array<Symbol> Also known as: inflection_kinds
Gets known strict inflection kinds.
202 203 204 |
# File 'lib/i18n-inflector/api_strict.rb', line 202 def kinds(locale=nil) each_kind(locale).to_a end |
#new_database(locale) ⇒ I18n::Inflector::InflectionData_Strict
Creates an empty strict inflections database for the specified locale.
72 73 74 75 76 |
# File 'lib/i18n-inflector/api_strict.rb', line 72 def new_database(locale) locale = prep_locale(locale) @inflected_locales_cache.clear @idb[locale] = I18n::Inflector::InflectionData_Strict.new(locale) end |
#prep_locale(locale = nil) ⇒ Symbol (protected)
If the locale
is not correct, it tries to use locale from I18n.locale and validates it as well.
Processes locale
identifier and validates whether it’s correct (not empty and not nil
).
653 654 655 656 657 |
# File 'lib/i18n-inflector/api_strict.rb', line 653 def prep_locale(locale=nil) locale ||= I18n.locale raise I18n::InvalidLocale.new(locale) if locale.to_s.empty? locale.to_sym end |
#tkl_args(token, kind, locale) ⇒ Array<Symbol,Symbol,Symbol> (protected) #tkl_args(token, kind) ⇒ Array<Symbol,Symbol,Symbol> (protected) #tkl_args(token) ⇒ Array<Symbol,Symbol,Symbol> (protected)
This method is the internal helper that prepares arguments containing token
, kind
and locale
.
632 633 634 635 636 637 638 639 640 641 642 |
# File 'lib/i18n-inflector/api_strict.rb', line 632 def tkl_args(args) token, kind, locale = case args.count when 1 then [args[0], nil, nil] when 2 then [args[0], args[1], nil] when 3 then args else raise I18n::ArgumentError.new("wrong number of arguments: #{args.count} for (1..3)") end token = token.nil? || token.to_s.empty? ? nil : token.to_sym kind = kind.nil? || kind.to_s.empty? ? nil : kind.to_sym [token,kind,locale] end |
#token_description(token, kind) ⇒ String? #token_description(token, kind, locale) ⇒ String?
If the given token
is really an alias it returns the description of the true token that it points to.
Gets the description of the given inflection token belonging to a strict kind.
589 590 591 592 593 |
# File 'lib/i18n-inflector/api_strict.rb', line 589 def token_description(*args) token, kind, locale = tkl_args(args) return nil if (token.nil? || kind.nil?) data_safe(locale).get_description(token, kind) end |
#tokens(kind) ⇒ Hash #tokens(kind, locale) ⇒ Hash
You cannot deduce where aliases are pointing to, since the information about a target is replaced by the description. To get targets use the #raw_tokens method. To simply list aliases and their targets use the #aliases method.
Gets available inflection tokens belonging to a strict kind and their descriptions.
421 422 423 |
# File 'lib/i18n-inflector/api_strict.rb', line 421 def tokens(kind=nil, locale=nil) each_token(kind, locale).to_h end |
#tokens_raw(kind) ⇒ Hash #tokens_raw(kind, locale) ⇒ Hash Also known as: raw_tokens
You may deduce whether the returned values are aliases or true tokens by testing if a value is a kind of Symbol or a String.
Gets available inflection tokens belonging to a strict kind and their values.
473 474 475 |
# File 'lib/i18n-inflector/api_strict.rb', line 473 def tokens_raw(kind=nil, locale=nil) each_token_raw(kind, locale).to_h end |
#tokens_true(kind) ⇒ Hash #tokens_true(kind, locale) ⇒ Hash Also known as: true_tokens
It returns only true tokens, not aliases.
Gets true inflection tokens belonging to a strict kind and their values.
522 523 524 |
# File 'lib/i18n-inflector/api_strict.rb', line 522 def tokens_true(kind=nil, locale=nil) each_token_true(kind, locale).to_h end |
#true_token(token, kind) ⇒ Symbol? #true_token(token, kind, locale) ⇒ Symbol? Also known as: resolve_alias
Gets true token for the given token
belonging to a strict kind. If the token is an alias it will be resolved and a true token (target) will be returned.
341 342 343 344 345 |
# File 'lib/i18n-inflector/api_strict.rb', line 341 def true_token(*args) token, kind, locale = tkl_args(args) return nil if (token.nil? || kind.nil?) data_safe(locale).get_true_token(token, kind) end |