Class: I18n::Inflector::InflectionData
- Inherits:
-
InflectionData_Strict
- Object
- InflectionData_Strict
- I18n::Inflector::InflectionData
- 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
Instance Method Summary collapse
-
#add_alias(name, target, kind = nil) ⇒ Boolean
Adds an alias (overwriting an existing alias).
-
#add_token(token, kind, description) ⇒ Boolean
Adds a token (overwriting existing token).
-
#each_alias(kind = nil) {|alias, target| ... } ⇒ LazyHashEnumerator
Iterates through all the aliases.
-
#each_raw_token(kind = nil) {|token, value| ... } ⇒ LazyHashEnumerator
Iterates through all the tokens in a way that it is possible to distinguish true tokens from aliases.
-
#each_token(kind = nil) {|token, description| ... } ⇒ Object
Iterates through all the tokens (including aliases).
-
#each_true_token(kind = nil) {|token, description| ... } ⇒ LazyHashEnumerator
Iterates through all the true tokens (not aliases).
-
#get_default_token(kind) ⇒ Symbol?
Reads the default token of a kind.
-
#get_description(token, kind = nil) ⇒ String?
Gets a description of a token or an alias.
-
#get_kind(token, kind = nil) ⇒ Symbol?
Gets a kind of the given token or alias.
-
#get_target_for_alias(alias_name, _kind = nil) ⇒ Symbol?
Gets a target token for the alias.
-
#get_true_token(token, kind = nil) ⇒ Symbol?
Gets a true token for the given identifier.
-
#has_alias?(alias_name, kind = nil) ⇒ Object
Tests if the given alias is really an alias.
-
#has_default_token?(kind) ⇒ Boolean
Tests if a kind has a default token assigned.
-
#has_kind?(kind) ⇒ Boolean
Tests if a kind exists.
-
#has_token?(token, kind = nil) ⇒ Object
Tests if a token (or alias) is present.
-
#has_true_token?(token, kind = nil) ⇒ Object
Tests if the token is a true token.
-
#initialize(locale = nil) ⇒ InflectionData
constructor
Initializes internal structures.
Methods inherited from InflectionData_Strict
#each_kind, #empty?, #set_default_token
Constructor Details
#initialize(locale = nil) ⇒ InflectionData
Initializes internal structures.
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).
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).
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_alias ⇒ LazyHashEnumerator #each_alias(kind) ⇒ LazyHashEnumerator
Iterates through all the aliases.
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_token ⇒ LazyHashEnumerator #each_raw_token(kind) ⇒ LazyHashEnumerator
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.
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_token ⇒ Object #each_token(kind) ⇒ LazyHashEnumerator
Use #each_raw_token if you want to distinguish true tokens from aliases.
Iterates through all the tokens (including aliases).
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_token ⇒ LazyHashEnumerator #each_true_token(kind) ⇒ LazyHashEnumerator
Iterates through all the true tokens (not aliases).
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?
It will always return true token (not an alias).
Reads the default token of a kind.
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?
If the token is really an alias it will resolve the alias first.
Gets a description of a token or an alias.
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.
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.
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?
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.
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.
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.
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.
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.
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.
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 |