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



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

def initialize(locale=nil)
  @kinds          = Hash.new(false)
  @tokens         = Hash.new(DUMMY_TOKEN)
  @lazy_tokens    = LazyHashEnumerator.new(@tokens)
  @lazy_kinds     = LazyArrayEnumerator.new(@kinds)
  @defaults       = Hash.new
  @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)



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

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.to_s.empty? unless kind.nil?
  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)



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

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:



194
195
196
197
198
199
# File 'lib/i18n-inflector/inflection_data.rb', line 194

def each_alias(kind=nil, &block)
  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(&block)
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:



220
221
222
223
224
225
# File 'lib/i18n-inflector/inflection_data.rb', line 220

def each_raw_token(kind=nil, &block)
  t = @lazy_tokens
  t = t.select  { |token,data| data[:kind] == kind } unless kind.nil?
  t.map         { |token,data| data[:target] || data[:description]  }.
  each(&block)
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:



244
245
246
247
248
# File 'lib/i18n-inflector/inflection_data.rb', line 244

def each_token(kind=nil, &block)
  t = @lazy_tokens
  t = t.select  { |token,data| data[:kind] == kind } unless kind.nil?
  t.map         { |token,data| data[:description]  }.each(&block)
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:



173
174
175
176
177
178
# File 'lib/i18n-inflector/inflection_data.rb', line 173

def each_true_token(kind=nil, &block)
  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(&block)
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



326
327
328
# File 'lib/i18n-inflector/inflection_data.rb', line 326

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



345
346
347
# File 'lib/i18n-inflector/inflection_data.rb', line 345

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



285
286
287
288
289
# File 'lib/i18n-inflector/inflection_data.rb', line 285

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



265
266
267
# File 'lib/i18n-inflector/inflection_data.rb', line 265

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



311
312
313
314
315
316
317
318
# File 'lib/i18n-inflector/inflection_data.rb', line 311

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



153
154
155
156
157
# File 'lib/i18n-inflector/inflection_data.rb', line 153

def has_alias?(alias_name, kind=nil)
  o = @tokens[alias_name]
  return false if o[:target].nil?
  kind.nil? ? true : 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



135
136
137
# File 'lib/i18n-inflector/inflection_data.rb', line 135

def has_default_token?(kind)
  @defaults.has_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



126
127
128
# File 'lib/i18n-inflector/inflection_data.rb', line 126

def has_kind?(kind)
  @kinds.has_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



117
118
119
120
# File 'lib/i18n-inflector/inflection_data.rb', line 117

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



95
96
97
98
99
100
# File 'lib/i18n-inflector/inflection_data.rb', line 95

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