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.
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).
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).
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_alias ⇒ LazyHashEnumerator #each_alias(kind) ⇒ LazyHashEnumerator
Iterates through all the aliases.
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_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.
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_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).
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_token ⇒ LazyHashEnumerator #each_true_token(kind) ⇒ LazyHashEnumerator
Iterates through all the true tokens (not aliases).
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?
It will always return true token (not an alias).
Reads the default token of a kind.
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?
If the token is really an alias it will resolve the alias first.
Gets a description of a token or an alias.
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.
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.
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?
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.
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.
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.
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.
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.
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.
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 |