Class: I18n::Inflector::InflectionData_Strict
- Inherits:
-
Object
- Object
- I18n::Inflector::InflectionData_Strict
- 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
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.
{}.freeze
Instance Attribute Summary collapse
-
#locale ⇒ Object
readonly
Locale this database works for.
Instance Method Summary collapse
-
#add_alias(name, target, kind) ⇒ Boolean
Adds an alias (overwriting existing alias).
-
#add_token(token, kind, description) ⇒ Boolean
Adds a token (overwriting existing token).
-
#each_alias(kind) {|alias, target| ... } ⇒ LazyHashEnumerator
Iterates through all the aliases of the given strict kind.
-
#each_kind {|kind| ... } ⇒ LazyArrayEnumerator
Iterates through all known strict kinds.
-
#each_raw_token(kind) {|token, value| ... } ⇒ LazyHashEnumerator
Iterates through all the tokens of the given strict kind in a way that it is possible to distinguish true tokens from aliases.
-
#each_token(kind) {|token, description| ... } ⇒ LazyHashEnumerator
Iterates through all the tokens (including aliases) of the given strict kind.
-
#each_true_token(kind) {|token, description| ... } ⇒ LazyHashEnumerator
Iterates through all the true tokens (not aliases) of the given strict kind.
-
#empty? ⇒ Boolean
Test if the inflection data have no elements.
-
#get_default_token(kind) ⇒ Symbol?
Reads the default token of a strict kind.
-
#get_description(token, kind) ⇒ String?
Gets a description of a token or alias belonging to a strict kind.
-
#get_kind(token, kind) ⇒ Symbol?
Gets a strict kind of the given token or alias.
-
#get_target_for_alias(alias_name, kind) ⇒ Symbol?
Gets a target token for the given alias of a strict kind.
-
#get_true_token(token, kind) ⇒ Symbol?
Gets a true token (of the given strict kind) for the given identifier.
-
#has_alias?(alias_name, kind) ⇒ Boolean
Tests if the given alias of the given strict kind is really an alias.
-
#has_default_token?(kind) ⇒ Boolean
Tests if the given strict kind has a default token assigned.
-
#has_kind?(kind) ⇒ Boolean
Tests if a strict kind exists.
-
#has_token?(token, kind) ⇒ Boolean
Tests if the given token (or alias) of the given strict kind is present.
-
#has_true_token?(token, kind) ⇒ Boolean
Tests if the given token of the given strict kind is a true token.
-
#initialize(locale = nil) ⇒ InflectionData_Strict
constructor
Initializes internal structures.
-
#set_default_token(kind, target) ⇒ void
Sets the default token for the given strict kind.
Constructor Details
#initialize(locale = nil) ⇒ InflectionData_Strict
Initializes internal structures.
39 40 41 42 43 44 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 39 def initialize(locale = nil) @tokens = Hash.new(DUMMY_TOKENS) @lazy_kinds = LazyArrayEnumerator.for(@tokens) @defaults = {} @locale = locale end |
Instance Attribute Details
#locale ⇒ Object (readonly)
Locale this database works for.
33 34 35 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 33 def locale @locale end |
Instance Method Details
#add_alias(name, target, kind) ⇒ Boolean
Adds an alias (overwriting existing alias).
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 53 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.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).
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 76 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] kind_tree = @tokens[kind] = Hash.new(DUMMY_TOKEN) if kind_tree.equal?(DUMMY_TOKENS) 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.
175 176 177 178 179 180 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 175 def each_alias(kind, &) LazyHashEnumerator.for(@tokens[kind]) .reject { |_token, data| data[:target].nil? } .map { |_token, data| data[:target] } .each(&) end |
#each_kind {|kind| ... } ⇒ LazyArrayEnumerator
Iterates through all known strict kinds.
266 267 268 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 266 def each_kind(&) @lazy_kinds.map { |k, _v| k }.each(&) end |
#each_raw_token(kind) {|token, value| ... } ⇒ LazyHashEnumerator
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.
194 195 196 197 198 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 194 def each_raw_token(kind, &) LazyHashEnumerator.for(@tokens[kind]) .map { |_token, data| data[:target] || data[:description] } .each(&) end |
#each_token(kind) {|token, description| ... } ⇒ LazyHashEnumerator
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.
211 212 213 214 215 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 211 def each_token(kind, &) LazyHashEnumerator.for(@tokens[kind]) .map { |_token, data| data[:description] } .each(&) end |
#each_true_token(kind) {|token, description| ... } ⇒ LazyHashEnumerator
Iterates through all the true tokens (not aliases) of the given strict kind.
160 161 162 163 164 165 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 160 def each_true_token(kind, &) LazyHashEnumerator.for(@tokens[kind]) .select { |_token, data| data[:target].nil? } .map { |_token, data| data[:description] } .each(&) end |
#empty? ⇒ Boolean
Test if the inflection data have no elements.
295 296 297 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 295 def empty? @tokens.empty? end |
#get_default_token(kind) ⇒ Symbol?
It will always return true token (not an alias).
Reads the default token of a strict kind.
276 277 278 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 276 def get_default_token(kind) @defaults[kind] end |
#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 alias belonging to a strict kind.
287 288 289 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 287 def get_description(token, kind) @tokens[kind][token][:description] end |
#get_kind(token, kind) ⇒ Symbol?
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.
237 238 239 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 237 def get_kind(token, kind) @tokens[kind].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.
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?
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.
252 253 254 255 256 257 258 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 252 def get_true_token(token, kind) o = @tokens[kind] return nil unless o.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.
147 148 149 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 147 def has_alias?(alias_name, kind) !@tokens[kind][alias_name][:target].nil? end |
#has_default_token?(kind) ⇒ Boolean
Tests if the given strict kind has a default token assigned.
136 137 138 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 136 def has_default_token?(kind) @defaults.key?(kind) end |
#has_kind?(kind) ⇒ Boolean
Tests if a strict kind exists.
126 127 128 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 126 def has_kind?(kind) @tokens.key?(kind) end |
#has_token?(token, kind) ⇒ Boolean
Tests if the given token (or alias) of the given strict kind is present.
118 119 120 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 118 def has_token?(token, kind) @tokens[kind].key?(token) end |
#has_true_token?(token, kind) ⇒ Boolean
Tests if the given token of the given strict kind is a true token.
106 107 108 |
# File 'lib/i18n-inflector/inflection_data_strict.rb', line 106 def has_true_token?(token, kind) @tokens[kind].key?(token) && @tokens[kind][token][:target].nil? end |
#set_default_token(kind, target) ⇒ void
This method returns an undefined value.
Sets the default token for the given strict kind.
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 |