Class: I18n::Inflector::InflectionData_Strict

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n-inflector/inflection_data_strict.rb

Overview

This class contains structures for keeping parsed translation data and basic operations for strict kinds and tokens assigned to them. Methods in this class vary from methods from InflectionData in a way that kind argument is usually required, not optional, since managing the strict kinds requires a kind of any token to be always known.

Direct Known Subclasses

InflectionData

Constant Summary collapse

DUMMY_TOKEN =

This constant contains a dummy hash for an empty token. It makes chaining calls to internal data easier.

{:kind=>nil, :target=>nil, :description=>nil}.freeze
DUMMY_TOKENS =

This constant contains a dummy hash of hashes for tokens collection. It makes chaining calls to internal data easier.

Hash.new(DUMMY_TOKEN).freeze
DUMMY_HASH =

This constant contains a dummy hash. It makes chaining calls to internal data easier.

Hash.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale = nil) ⇒ InflectionData_Strict

Initializes internal structures.

Parameters:

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

    the locale identifier for the object to be labeled with



40
41
42
43
44
45
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 40

def initialize(locale=nil)
  @tokens       = Hash.new(DUMMY_TOKENS)
  @lazy_kinds   = LazyArrayEnumerator.for(@tokens)
  @defaults     = Hash.new
  @locale       = locale
end

Instance Attribute Details

#localeObject (readonly)

Locale this database works for.



34
35
36
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 34

def locale
  @locale
end

Instance Method Details

#add_alias(name, target, kind) ⇒ Boolean

Adds an alias (overwriting existing alias).

Parameters:

  • name (Symbol)

    the name of an alias

  • target (Symbol)

    the target token for the created alias

  • kind (Symbol)

    the identifier of a kind

Returns:

  • (Boolean)

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



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 54

def add_alias(name, target, kind)
  return false if (name.nil? || target.nil? || kind.nil?)
  return false if (name.to_s.empty? || target.to_s.empty? || kind.to_s.empty?)
  name    = name.to_sym
  target  = target.to_sym
  kind    = kind.to_sym
  k       = @tokens[kind]
  return false unless k.has_key?(target)
  token               = k[name] = {}
  token[:description] = k[target][:description]
  token[:target]      = target
  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 identifier of a kind

  • description (String)

    the description of a token

Returns:

  • (Boolean)

    true if everything went ok, false otherwise (in case of bad names)



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 75

def add_token(token, kind, description)
  return false if (token.to_s.empty? || kind.to_s.empty? || description.nil?)
  token     = token.to_sym
  kind      = kind.to_sym
  kind_tree = @tokens[kind]
  if kind_tree.equal?(DUMMY_TOKENS)
    kind_tree = @tokens[kind] = Hash.new(DUMMY_TOKEN)
  end
  token = kind_tree[token] = {}
  token[:description] = description.to_s
  true
end

#each_alias(kind) {|alias, target| ... } ⇒ LazyHashEnumerator

Iterates through all the aliases of the given strict kind.

Parameters:

  • kind (Symbol)

    the identifier of a kind

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:



175
176
177
178
179
180
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 175

def each_alias(kind, &block)
  LazyHashEnumerator.for(@tokens[kind]).
  reject { |token,data| data[:target].nil?  }.
  map    { |token,data| data[:target]       }.
  each(&block)
end

#each_kind {|kind| ... } ⇒ LazyArrayEnumerator

Iterates through all known strict kinds.

Yields:

  • (kind)

    optional block in which each kind will be yielded

Yield Parameters:

  • kind (Symbol)

    the inflection kind

Yield Returns:

Returns:



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

def each_kind(&block)
  @lazy_kinds.map{|k,v| k}.each(&block)
end

#each_raw_token(kind) {|token, value| ... } ⇒ LazyHashEnumerator

Note:

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

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

Parameters:

  • kind (Symbol)

    the identifier of a kind

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:



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

def each_raw_token(kind, &block)
  LazyHashEnumerator.for(@tokens[kind]).
  map { |token,data| data[:target] || data[:description] }.
  each(&block)
end

#each_token(kind) {|token, description| ... } ⇒ LazyHashEnumerator

Note:

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

Iterates through all the tokens (including aliases) of the given strict kind.

Parameters:

  • kind (Symbol)

    the identifier of a kind

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:



211
212
213
214
215
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 211

def each_token(kind, &block)
  LazyHashEnumerator.for(@tokens[kind]).
  map{ |token,data| data[:description] }.
  each(&block)
end

#each_true_token(kind) {|token, description| ... } ⇒ LazyHashEnumerator

Iterates through all the true tokens (not aliases) of the given strict kind.

Parameters:

  • kind (Symbol)

    the identifier of a kind

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:



160
161
162
163
164
165
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 160

def each_true_token(kind, &block)
  LazyHashEnumerator.for(@tokens[kind]).
  select { |token,data| data[:target].nil?  }.
  map    { |token,data| data[:description]  }.
  each(&block)
end

#empty?Boolean

Test if the inflection data have no elements.

Returns:

  • (Boolean)

    true if the inflection data have no elements



294
295
296
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 294

def empty?
  @tokens.empty?
end

#get_default_token(kind) ⇒ Symbol?

Note:

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

Reads the default token of a strict 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



275
276
277
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 275

def get_default_token(kind)
  @defaults[kind]
end

#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 alias belonging to a strict 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



286
287
288
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 286

def get_description(token, kind)
  @tokens[kind][token][:description]
end

#get_kind(token, kind) ⇒ Symbol?

Note:

This method may be concidered dummy since there is a need to give the inflection kind, but it’s here in order to preserve compatibility with the same method from I18n::Inflector::InflectionData which guesses the kind.

Gets a strict kind of the given token or alias.

Parameters:

  • token (Symbol)

    identifier of a token

  • kind (Symbol)

    the identifier of a kind (expectations filter)

Returns:

  • (Symbol, nil)

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



237
238
239
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 237

def get_kind(token, kind)
  @tokens[kind].has_key?(token) ? kind : nil
end

#get_target_for_alias(alias_name, kind) ⇒ Symbol?

Gets a target token for the given alias of a strict kind.

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



223
224
225
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 223

def get_target_for_alias(alias_name, kind)
  @tokens[kind][alias_name][:target]
end

#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 (of the given strict kind) for the given identifier.

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



252
253
254
255
256
257
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 252

def get_true_token(token, kind)
  o = @tokens[kind]
  return nil unless o.has_key?(token)
  o = o[token]
  o[:target].nil? ? token : o[:target]
end

#has_alias?(alias_name, kind) ⇒ Boolean

Tests if the given alias of the given strict kind is really an alias.

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



147
148
149
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 147

def has_alias?(alias_name, kind)
  not @tokens[kind][alias_name][:target].nil?
end

#has_default_token?(kind) ⇒ Boolean

Tests if the given strict 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



136
137
138
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 136

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

#has_kind?(kind) ⇒ Boolean

Tests if a strict 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_strict.rb', line 126

def has_kind?(kind)
  @tokens.has_key?(kind)
end

#has_token?(token, kind) ⇒ Boolean

Tests if the given token (or alias) of the given strict kind is present.

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



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

def has_token?(token, kind)
 @tokens[kind].has_key?(token)
end

#has_true_token?(token, kind) ⇒ Boolean

Tests if the given token of the given strict kind is a true token.

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



106
107
108
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 106

def has_true_token?(token, kind)
  @tokens[kind].has_key?(token) && @tokens[kind][token][:target].nil?
end

#set_default_token(kind, target)

This method returns an undefined value.

Sets the default token for the given strict kind.

Parameters:

  • kind (Symbol)

    the kind to which the default token should be assigned

  • target (Symbol)

    the token to set



94
95
96
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 94

def set_default_token(kind, target)
  @defaults[kind.to_sym] = target.to_sym
end