Class: I18n::Inflector::InflectionData

Inherits:
InflectionData_Strict show all
Defined in:
lib/i18n-inflector/inflection_data.rb

Overview

This class contains structures for keeping parsed translation data and basic operations for performing on them.

Constant Summary

Constants inherited from InflectionData_Strict

I18n::Inflector::InflectionData_Strict::DUMMY_HASH, I18n::Inflector::InflectionData_Strict::DUMMY_TOKEN, I18n::Inflector::InflectionData_Strict::DUMMY_TOKENS

Instance Attribute Summary

Attributes inherited from InflectionData_Strict

#locale

Instance Method Summary collapse

Methods inherited from InflectionData_Strict

#each_kind, #empty?, #set_default_token

Constructor Details

#initialize(locale = nil) ⇒ InflectionData

Initializes internal structures.

Parameters:

  • locale (Symbol, nil) (defaults to: nil)

    the locale identifier for the object to be labeled with



20
21
22
23
24
25
26
27
# File 'lib/i18n-inflector/inflection_data.rb', line 20

def initialize(locale = nil)
  @kinds          = Hash.new(false)
  @tokens         = Hash.new(DUMMY_TOKEN)
  @lazy_tokens    = LazyHashEnumerator.for(@tokens)
  @lazy_kinds     = LazyArrayEnumerator.for(@kinds)
  @defaults       = {}
  @locale         = locale
end

Instance Method Details

#add_alias(name, target) ⇒ Boolean #add_alias(name, target, kind) ⇒ Boolean

Adds an alias (overwriting an existing alias).

Overloads:

  • #add_alias(name, target) ⇒ Boolean

    Adds an alias (overwriting an existing alias).

    Parameters:

    • name (Symbol)

      the name of an alias

    • target (Symbol)

      the target token for the given alias

    Returns:

    • (Boolean)

      true if everything went ok, false otherwise (in case of bad or nil names or non-existent targets)

  • #add_alias(name, target, kind) ⇒ Boolean

    Adds an alias (overwriting an existing alias) if the given kind matches the kind of the given target.

    Parameters:

    • name (Symbol)

      the name of an alias

    • target (Symbol)

      the target token for the given alias

    • kind (Symbol)

      the optional kind of a taget

    Returns:

    • (Boolean)

      true if everything went ok, false otherwise (in case of bad or nil names or non-existent targets)

Returns:

  • (Boolean)

    true if everything went ok, false otherwise (in case of bad or nil names or non-existent targets)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/i18n-inflector/inflection_data.rb', line 47

def add_alias(name, target, kind = nil)
  target  = target.to_s
  name    = name.to_s
  return false if name.empty? || target.empty?

  kind    = nil if !kind.nil? && !kind.nil? && kind.to_s.empty?
  name    = name.to_sym
  target  = target.to_sym
  t_kind  = get_kind(target)
  return false if t_kind.nil? || (!kind.nil? && t_kind != kind)

  @tokens[name] = {}
  @tokens[name][:kind]        = kind
  @tokens[name][:target]      = target
  @tokens[name][:description] = @tokens[target][:description]
  true
end

#add_token(token, kind, description) ⇒ Boolean

Adds a token (overwriting existing token).

Parameters:

  • token (Symbol)

    the name of a token to add

  • kind (Symbol)

    the kind of a token

  • description (String)

    the description of a token

Returns:

  • (Boolean)

    true if everything went ok, false otherwise (in case of bad names or non-existent targets)



72
73
74
75
76
77
78
79
80
# File 'lib/i18n-inflector/inflection_data.rb', line 72

def add_token(token, kind, description)
  return false if token.to_s.empty? || kind.to_s.empty? || description.nil?

  token = token.to_sym
  @tokens[token] = {}
  @tokens[token][:kind]         = kind.to_sym
  @tokens[token][:description]  = description.to_s
  @kinds[kind] = true
end

#each_aliasLazyHashEnumerator #each_alias(kind) ⇒ LazyHashEnumerator

Iterates through all the aliases.

Overloads:

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:

Returns:



198
199
200
201
202
203
# File 'lib/i18n-inflector/inflection_data.rb', line 198

def each_alias(kind = nil, &)
  t = @lazy_tokens
  t = t.select  { |_token, data| data[:kind] == kind  } unless kind.nil?
  t.reject      { |_token, data| data[:target].nil?   }
    .map         { |_token, data| data[:target]        }.each(&)
end

#each_raw_tokenLazyHashEnumerator #each_raw_token(kind) ⇒ LazyHashEnumerator

Note:

True tokens have descriptions (String) and aliases have targets (Symbol) assigned.

Iterates through all the tokens in a way that it is possible to distinguish true tokens from aliases.

Overloads:

  • #each_raw_tokenLazyHashEnumerator

    Reads all the tokens in a way that it is possible to distinguish true tokens from aliases.

    Returns:

  • #each_raw_token(kind) ⇒ LazyHashEnumerator

    Reads all the tokens of the given kind in a way that it is possible to distinguish true tokens from aliases.

    Parameters:

    • kind (Symbol)

      the identifier of a kind

    Returns:

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:

Returns:



224
225
226
227
228
229
# File 'lib/i18n-inflector/inflection_data.rb', line 224

def each_raw_token(kind = nil, &)
  t = @lazy_tokens
  t = t.select  { |_token, data| data[:kind] == kind } unless kind.nil?
  t.map         { |_token, data| data[:target] || data[:description] }
    .each(&)
end

#each_tokenObject #each_token(kind) ⇒ LazyHashEnumerator

Note:

Use #each_raw_token if you want to distinguish true tokens from aliases.

Iterates through all the tokens (including aliases).

Overloads:

  • #each_tokenObject

    Reads all the tokens (including aliases).

  • #each_token(kind) ⇒ LazyHashEnumerator

    Reads all the tokens (including aliases) of the given kind.

    Parameters:

    • kind (Symbol)

      the identifier of a kind

    Returns:

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:



248
249
250
251
252
# File 'lib/i18n-inflector/inflection_data.rb', line 248

def each_token(kind = nil, &)
  t = @lazy_tokens
  t = t.select  { |_token, data| data[:kind] == kind } unless kind.nil?
  t.map         { |_token, data| data[:description]  }.each(&)
end

#each_true_tokenLazyHashEnumerator #each_true_token(kind) ⇒ LazyHashEnumerator

Iterates through all the true tokens (not aliases).

Overloads:

  • #each_true_tokenLazyHashEnumerator

    Reads all the true tokens (not aliases).

    Returns:

  • #each_true_token(kind) ⇒ LazyHashEnumerator

    Reads all the true tokens (not aliases) of the given kind.

    Parameters:

    • kind (Symbol)

      the identifier of a kind

    Returns:

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:

Returns:



177
178
179
180
181
182
# File 'lib/i18n-inflector/inflection_data.rb', line 177

def each_true_token(kind = nil, &)
  t = @lazy_tokens
  t = t.select  { |_token, data| data[:kind] == kind  } unless kind.nil?
  t.select      { |_token, data| data[:target].nil?   }
    .map         { |_token, data| data[:description]   }.each(&)
end

#get_default_token(kind) ⇒ Symbol?

Note:

It will always return true token (not an alias).

Reads the default token of a kind.

Parameters:

  • kind (Symbol)

    the identifier of a kind

Returns:

  • (Symbol, nil)

    the default token of the given kind or nil if there is no default token set



333
334
335
# File 'lib/i18n-inflector/inflection_data.rb', line 333

def get_default_token(kind)
  @defaults[kind]
end

#get_description(token) ⇒ String? #get_description(token, kind) ⇒ String?

Note:

If the token is really an alias it will resolve the alias first.

Gets a description of a token or an alias.

Overloads:

  • #get_description(token) ⇒ String?

    Gets a description of a token or an alias.

    Parameters:

    • token (Symbol)

      the identifier of a token

    Returns:

    • (String, nil)

      the string containing description of the given token (which may be an alias) or nil if the token is unknown

  • #get_description(token, kind) ⇒ String?

    Gets a description of a token or an alias of the given kind

    Parameters:

    • token (Symbol)

      the identifier of a token

    • kind (Symbol)

      the identifier of a kind

    Returns:

    • (String, nil)

      the string containing description of the given token (which may be an alias) or nil if the token is unknown

Returns:

  • (String, nil)

    the string containing description of the given token (which may be an alias) or nil if the token is unknown



352
353
354
# File 'lib/i18n-inflector/inflection_data.rb', line 352

def get_description(token, kind = nil)
  @tokens[token][:description] if kind.nil? || @tokens[token][:kind] == kind
end

#get_kind(token) ⇒ Symbol? #get_kind(token, kind) ⇒ Symbol?

Gets a kind of the given token or alias.

Overloads:

  • #get_kind(token) ⇒ Symbol?

    Gets a kind of the given token or alias.

    Parameters:

    • token (Symbol)

      identifier of a token

    Returns:

    • (Symbol, nil)

      the kind of the given token or nil if the token is unknown

  • #get_kind(token, kind) ⇒ Symbol?

    Gets a kind of the given token or alias. The kind will work as the expectation filter.

    Parameters:

    • token (Symbol)

      identifier of a token

    • kind (Symbol)

      the identifier of a kind

    Returns:

    • (Symbol, nil)

      the kind of the given token or nil if the token is unknown

Returns:

  • (Symbol, nil)

    the kind of the given token or nil if the token is unknown



289
290
291
292
293
294
# File 'lib/i18n-inflector/inflection_data.rb', line 289

def get_kind(token, kind = nil)
  k = @tokens[token][:kind]
  return k if kind.nil? || kind == k

  nil
end

#get_target_for_alias(alias_name) ⇒ Symbol? #get_target_for_alias(alias_name, kind) ⇒ Symbol?

Gets a target token for the alias.

Overloads:

  • #get_target_for_alias(alias_name) ⇒ Symbol?

    Gets a target token for the alias.

    Parameters:

    • alias_name (Symbol)

      the identifier of an alias

    Returns:

    • (Symbol, nil)

      the token that the given alias points to or nil if it isn’t really an alias

  • #get_target_for_alias(alias_name, kind) ⇒ Symbol?

    Gets a target token for the alias that’s kind is given.

    Parameters:

    • alias_name (Symbol)

      the identifier of an alias

    • kind (Symbol)

      the identifier of a kind

    Returns:

    • (Symbol, nil)

      the token that the given alias points to or nil if it isn’t really an alias

Returns:

  • (Symbol, nil)

    the token that the given alias points to or nil if it isn’t really an alias



269
270
271
# File 'lib/i18n-inflector/inflection_data.rb', line 269

def get_target_for_alias(alias_name, _kind = nil)
  @tokens[alias_name][:target]
end

#get_true_token(token) ⇒ Symbol? #get_true_token(token, kind) ⇒ Symbol?

Note:

If the given token is really an alias it will be resolved and the real token pointed by that alias will be returned.

Gets a true token for the given identifier.

Overloads:

  • #get_true_token(token) ⇒ Symbol?

    Gets a true token for the given token identifier.

    Parameters:

    • token (Symbol)

      the identifier of a token

    Returns:

    • (Symbol, nil)

      the true token for the given token or nil if the token is unknown

  • #get_true_token(token, kind) ⇒ Symbol?

    Gets a true token for the given token identifier and the given kind. The kind will work as the expectation filter.

    Parameters:

    • token (Symbol)

      the identifier of a token

    • kind (Symbol)

      the identifier of a kind

    Returns:

    • (Symbol, nil)

      the true token for the given token or nil if the token is unknown or is not a kind of the given kind

Returns:

  • (Symbol, nil)

    the true token for the given token or nil



316
317
318
319
320
321
322
323
324
325
# File 'lib/i18n-inflector/inflection_data.rb', line 316

def get_true_token(token, kind = nil)
  o = @tokens[token]
  k = o[:kind]
  return nil if k.nil?

  r = o[:target] || token
  return r if kind.nil?

  (k == kind) ? r : nil
end

#has_alias?(alias_name) ⇒ Boolean #has_alias?(alias_name, kind) ⇒ Boolean

Tests if the given alias is really an alias.

Overloads:

  • #has_alias?(alias_name) ⇒ Boolean

    Tests if the given alias is really an alias.

    Parameters:

    • alias_name (Symbol)

      the identifier of an alias

    Returns:

    • (Boolean)

      true if the given alias is really an alias, false otherwise

  • #has_alias?(alias_name, kind) ⇒ Boolean

    Tests if the given alias is really an alias. The kind will work as the expectation filter.

    Parameters:

    • alias_name (Symbol)

      the identifier of an alias

    • kind (Symbol)

      the identifier of a kind

    Returns:

    • (Boolean)

      true if the given alias is really an alias being a kind of the given kind, false otherwise



156
157
158
159
160
161
# File 'lib/i18n-inflector/inflection_data.rb', line 156

def has_alias?(alias_name, kind = nil)
  o = @tokens[alias_name]
  return false if o[:target].nil?

  kind.nil? || o[:kind] == kind
end

#has_default_token?(kind) ⇒ Boolean

Tests if a kind has a default token assigned.

Parameters:

  • kind (Symbol)

    the identifier of a kind

Returns:

  • (Boolean)

    true if there is a default token of the given kind



138
139
140
# File 'lib/i18n-inflector/inflection_data.rb', line 138

def has_default_token?(kind)
  @defaults.key?(kind)
end

#has_kind?(kind) ⇒ Boolean

Tests if a kind exists.

Parameters:

  • kind (Symbol)

    the identifier of a kind

Returns:

  • (Boolean)

    true if the given kind exists



129
130
131
# File 'lib/i18n-inflector/inflection_data.rb', line 129

def has_kind?(kind)
  @kinds.key?(kind)
end

#has_token(token) ⇒ Boolean #has_token(token, kind) ⇒ Boolean

Tests if a token (or alias) is present.

Overloads:

  • #has_token(token) ⇒ Boolean

    Tests if a token (or alias) is present.

    Parameters:

    • token (Symbol)

      the identifier of a token

    Returns:

    • (Boolean)

      true if the given token (which may be an alias) exists

  • #has_token(token, kind) ⇒ Boolean

    Tests if a token (or alias) is present. The kind will work as the expectation filter.

    Parameters:

    • token (Symbol)

      the identifier of a token

    • kind (Symbol)

      the identifier of a kind

    Returns:

    • (Boolean)

      true if the given token (which may be an alias) exists and if kind of the given kind



120
121
122
123
# File 'lib/i18n-inflector/inflection_data.rb', line 120

def has_token?(token, kind = nil)
  k = @tokens[token][:kind]
  kind.nil? ? !k.nil? : k == kind
end

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

Tests if the token is a true token.

Overloads:

  • #has_true_token?(token) ⇒ Boolean

    Tests if the token is a true token.

    Parameters:

    • token (Symbol)

      the identifier of a token

    Returns:

    • (Boolean)

      true if the given token is a token and not an alias, false otherwise

  • #has_true_token?(token, kind) ⇒ Boolean

    Tests if the token is a true token. The kind will work as the expectation filter.

    Parameters:

    • token (Symbol)

      the identifier of a token

    • kind (Symbol)

      the identifier of a kind

    Returns:

    • (Boolean)

      true if the given token is a token and not an alias, and is a kind of the given kind, false otherwise



97
98
99
100
101
102
103
# File 'lib/i18n-inflector/inflection_data.rb', line 97

def has_true_token?(token, kind = nil)
  o = @tokens[token]
  k = o[:kind]
  return false if k.nil? || !o[:target].nil?

  kind.nil? || k == kind
end