Class: I18n::Inflector::API_Strict

Inherits:
Object
  • Object
show all
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

API

Instance Method Summary collapse

Constructor Details

#initialize(idb = nil, options = nil) ⇒ API_Strict

Note:

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, options = nil)
  @idb      = idb.nil?      ? {} : idb
  @options  = options.nil?  ? I18n::Inflector::InflectionOptions.new : options
  @lazy_locales = LazyHashEnumerator.for(@idb)
  @inflected_locales_cache = {}
end

Instance Method Details

#add_database(db) ⇒ I18n::Inflector::InflectionData_Strict

Note:

It doesn't create copy of inflection database, it registers the given object.

Attaches InflectionData_Strict instance to the current object.

Raises:



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.

Overloads:

  • #aliases(kind) ⇒ Hash

    Gets inflection aliases (and their pointers) of the given kind and the current locale.

  • #aliases(kind, locale) ⇒ Hash

    Gets inflection aliases (and their pointers) of the given kind and locale.

Raises:



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.

Overloads:

  • #default_token(kind) ⇒ Symbol?

    This method reads default token of the given kind and the current locale.

  • #default_token(kind, locale) ⇒ Symbol?

    This method reads default token of the given kind and the given locale.

Raises:



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

Note:

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.

Raises:



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.

Overloads:

  • #each_alias(kind) ⇒ LazyHashEnumerator

    Iterates through inflection aliases (and their pointers) of the given kind and the current locale.

  • #each_alias(kind, locale) ⇒ LazyHashEnumerator

    Iterates through inflection aliases (and their pointers) of the given kind and locale.

Yields:

  • (alias, target)

    optional block in which each alias will be yielded

Yield Parameters:

  • alias (Symbol)

    an alias

  • target (Symbol)

    a name of the target token

Yield Returns:

Raises:



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_localeLazyArrayEnumerator #each_inflected_locale(kind) ⇒ LazyArrayEnumerator Also known as: each_locale, each_supported_locale

Iterates through locales which have configured strict inflection support.

Overloads:

  • #each_inflected_localeLazyArrayEnumerator

    Iterates through locales which have configured inflection support.

  • #each_inflected_locale(kind) ⇒ LazyArrayEnumerator

    Iterates through locales which have configured inflection support for the given kind.

Yields:

  • (locale)

    optional block in which each kind will be yielded

Yield Parameters:

  • locale (Symbol)

    the inflected locale identifier

Yield Returns:



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

#kindsLazyArrayEnumerator #kinds(locale) ⇒ LazyArrayEnumerator Also known as: each_inflection_kind

Iterates through known strict inflection kinds.

Overloads:

  • #kindsLazyArrayEnumerator

    Iterates through known inflection kinds for the current locale.

  • #kinds(locale) ⇒ LazyArrayEnumerator

    Iterates through known inflection kinds for the given locale.

Yields:

  • (kind)

    optional block in which each kind will be yielded

Yield Parameters:

  • kind (Symbol)

    the inflection kind

Yield Returns:

Raises:



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

Note:

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.

Overloads:

  • #each_token(kind) ⇒ LazyHashEnumerator

    Iterates through available inflection tokens and their descriptions for some kind and the current locale.

  • #each_token(kind, locale) ⇒ LazyHashEnumerator

    Iterates through available inflection tokens and their descriptions of the given kind and locale.

Yields:

  • (token, description)

    optional block in which each token will be yielded

Yield Parameters:

  • token (Symbol)

    a token

  • description (String)

    a description string for a token

Yield Returns:

Raises:



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

Note:

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.

Overloads:

  • #each_token_raw(kind) ⇒ LazyHashEnumerator

    Iterates through available inflection tokens and their values of the given kind and the current locale.

  • #each_token_raw(kind, locale) ⇒ LazyHashEnumerator

    Iterates through available inflection tokens (and their values) of the given kind and locale.

Yields:

  • (token, value)

    optional block in which each token will be yielded

Yield Parameters:

  • token (Symbol)

    a token

  • value (Symbol, String)

    a description string for a token or a target (if alias)

Yield Returns:

Raises:



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

Note:

It returns only true tokens, not aliases.

Iterates through inflection tokens belonging to a strict kind and their values.

Overloads:

  • #each_token_true(kind) ⇒ LazyHashEnumerator

    Iterates through true inflection tokens (and their values) of the given kind and the current locale.

  • #each_token_true(kind, locale) ⇒ LazyHashEnumerator

    Iterates through true inflection tokens (and their values) of the given kind and locale.

Yields:

  • (token, description)

    optional block in which each token will be yielded

Yield Parameters:

  • token (Symbol)

    a token

  • description (String)

    a description string for a token

Yield Returns:

Raises:



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.

Overloads:

  • #has_alias?(token, kind) ⇒ Boolean

    Uses the current locale and the given kind to check if the given token is an alias.

  • #has_alias?(token, kind, locale) ⇒ Boolean

    Uses the given locale and kind to check if the given token is an alias.

Raises:



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.

Overloads:

  • #has_kind?(kind) ⇒ Boolean

    Tests if a strict kind exists for the current locale.

  • #has_kind?(kind, locale) ⇒ Boolean

    Tests if a strict kind exists for the given locale.

Raises:



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.

Overloads:

  • #has_token?(token, kind) ⇒ Boolean

    Uses the current locale and the given kind kind to check if the given token exists.

  • #has_token?(token, kind, locale) ⇒ Boolean

    Uses the given locale and kind to check if the given token exists.

Raises:



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).

Overloads:

  • #has_true_token?(token, kind) ⇒ Boolean

    Uses the current locale and the given kind to check if the given token is a true token.

  • #has_true_token?(token, kind, locale) ⇒ Boolean

    Uses the given locale and kind to check if the given token is a true token.

Raises:



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.

Overloads:

  • #inflected_locale?Boolean

    Checks if the current locale was configured to support inflection.

  • #inflected_locale?(locale) ⇒ Boolean

    Checks if the given locale was configured to support inflection.

Raises:



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_localesArray<Symbol> #inflected_locales(kind) ⇒ Array<Symbol> Also known as: locales, supported_locales

Gets locales which have configured strict inflection support.

Overloads:

  • #inflected_localesArray<Symbol>

    Gets locales which have configured inflection support.

  • #inflected_locales(kind) ⇒ Array<Symbol>

    Gets locales which have configured inflection support for the given kind.



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.

Overloads:

  • #kind(token, kind) ⇒ Symbol?

    Uses current locale and the given kind to get a kind of the given token (which may be an alias).

  • #kind(token, kind, locale) ⇒ Symbol?

    Uses the given locale to get a kind of the given token (which may be an alias).

Raises:



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

#kindsArray<Symbol> #kinds(locale) ⇒ Array<Symbol> Also known as: inflection_kinds

Gets known strict inflection kinds.

Overloads:

  • #kindsArray<Symbol>

    Gets known inflection kinds for the current locale.

  • #kinds(locale) ⇒ Array<Symbol>

    Gets known inflection kinds for the given locale.

Raises:



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.

Raises:



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?

Note:

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.

Overloads:

  • #token_description(token, kind) ⇒ String?

    Uses the current locale and the given token to get a description of that token.

  • #token_description(token, kind, locale) ⇒ String?

    Uses the given locale and the given token to get a description of that token.

Raises:



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

Note:

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.

Overloads:

  • #tokens(kind) ⇒ Hash

    Gets available inflection tokens and their descriptions for some kind and the current locale.

  • #tokens(kind, locale) ⇒ Hash

    Gets available inflection tokens and their descriptions of the given kind and locale.

Raises:



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

Note:

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.

Overloads:

  • #tokens_raw(kind) ⇒ Hash

    Gets available inflection tokens and their values of the given kind and the current locale.

  • #tokens_raw(kind, locale) ⇒ Hash

    Gets available inflection tokens (and their values) of the given kind and locale.

Raises:



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

Note:

It returns only true tokens, not aliases.

Gets true inflection tokens belonging to a strict kind and their values.

Overloads:

  • #tokens_true(kind) ⇒ Hash

    Gets true inflection tokens (and their values) of the given kind and the current locale.

  • #tokens_true(kind, locale) ⇒ Hash

    Gets true inflection tokens (and their values) of the given kind and locale.

Raises:



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.

Overloads:

  • #true_token(token, kind) ⇒ Symbol?

    Uses the current locale and the given kind to get a real token for the given token. If the token is an alias it will be resolved and a true token (target) will be returned.

  • #true_token(token, kind, locale) ⇒ Symbol?

    Uses the given locale and kind to get a real token for the given token. If the token is an alias it will be resolved and a true token (target) will be returned.

Raises:



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