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) ⇒ void
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
tokenbelonging 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
tokenbelonging to a strict kind exists. -
#has_true_token?(*args) ⇒ Boolean
(also: #token_is_true?)
Checks if the given
tokenbelonging 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.
-
#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
tokenbelonging 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.
55 56 57 58 59 60 |
# File 'lib/i18n-inflector/api_strict.rb', line 55 def initialize(idb = nil, = nil) @idb = idb.nil? ? {} : idb @options = .nil? ? I18n::Inflector::InflectionOptions.new : @lazy_locales = LazyHashEnumerator.for(@idb) @inflected_locales_cache = {} 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.
85 86 87 88 89 90 91 92 |
# File 'lib/i18n-inflector/api_strict.rb', line 85 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.
573 574 575 |
# File 'lib/i18n-inflector/api_strict.rb', line 573 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.
245 246 247 248 249 |
# File 'lib/i18n-inflector/api_strict.rb', line 245 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) ⇒ void
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.
102 103 104 105 106 107 108 |
# File 'lib/i18n-inflector/api_strict.rb', line 102 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.
554 555 556 557 |
# File 'lib/i18n-inflector/api_strict.rb', line 554 def each_alias(kind = nil, locale = nil, &) kind = kind.to_s.empty? ? nil : kind.to_sym data_safe(locale).each_alias(kind, &) 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.
144 145 146 147 148 149 |
# File 'lib/i18n-inflector/api_strict.rb', line 144 def each_inflected_locale(kind = nil, &) 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(&) end |
#kinds ⇒ LazyArrayEnumerator #kinds(locale) ⇒ LazyArrayEnumerator Also known as: each_inflection_kind
Iterates through known strict inflection kinds.
187 188 189 |
# File 'lib/i18n-inflector/api_strict.rb', line 187 def each_kind(locale = nil, &) data_safe(locale).each_kind(&) 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.
403 404 405 406 |
# File 'lib/i18n-inflector/api_strict.rb', line 403 def each_token(kind = nil, locale = nil, &) kind = kind.to_s.empty? ? nil : kind.to_sym data_safe(locale).each_token(kind, &) 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.
455 456 457 458 |
# File 'lib/i18n-inflector/api_strict.rb', line 455 def each_token_raw(kind = nil, locale = nil, &) kind = kind.to_s.empty? ? nil : kind.to_sym data_safe(locale).each_raw_token(kind, &) 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.
507 508 509 510 |
# File 'lib/i18n-inflector/api_strict.rb', line 507 def each_token_true(kind = nil, locale = nil, &) kind = kind.to_s.empty? ? nil : kind.to_sym data_safe(locale).each_true_token(kind, &) 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.
268 269 270 271 272 273 |
# File 'lib/i18n-inflector/api_strict.rb', line 268 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.
223 224 225 226 227 |
# File 'lib/i18n-inflector/api_strict.rb', line 223 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.
318 319 320 321 322 323 |
# File 'lib/i18n-inflector/api_strict.rb', line 318 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).
293 294 295 296 297 298 |
# File 'lib/i18n-inflector/api_strict.rb', line 293 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 125 126 |
# File 'lib/i18n-inflector/api_strict.rb', line 122 def inflected_locale?(locale = nil) !@idb[prep_locale(locale)].nil? rescue StandardError 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.
164 165 166 167 168 |
# File 'lib/i18n-inflector/api_strict.rb', line 164 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.
373 374 375 376 377 |
# File 'lib/i18n-inflector/api_strict.rb', line 373 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.
204 205 206 |
# File 'lib/i18n-inflector/api_strict.rb', line 204 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.
70 71 72 73 74 |
# File 'lib/i18n-inflector/api_strict.rb', line 70 def new_database(locale) locale = prep_locale(locale) @inflected_locales_cache.clear @idb[locale] = I18n::Inflector::InflectionData_Strict.new(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.
598 599 600 601 602 603 |
# File 'lib/i18n-inflector/api_strict.rb', line 598 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.
430 431 432 |
# File 'lib/i18n-inflector/api_strict.rb', line 430 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.
482 483 484 |
# File 'lib/i18n-inflector/api_strict.rb', line 482 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.
531 532 533 |
# File 'lib/i18n-inflector/api_strict.rb', line 531 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.
348 349 350 351 352 353 |
# File 'lib/i18n-inflector/api_strict.rb', line 348 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 |