i18n-inflector 2.0.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +1 -0
- data/ChangeLog +412 -0
- data/Gemfile +1 -0
- data/Manifest.txt +6 -1
- data/README.rdoc +16 -11
- data/Rakefile +15 -2
- data/ci/i18n-inflector.gemspec +1 -1
- data/ci/i18nv4-Gemfile +15 -0
- data/docs/EXAMPLES +222 -0
- data/docs/HISTORY +31 -0
- data/docs/LEGAL +0 -1
- data/docs/RELATIONS +16 -13
- data/docs/TODO +25 -3
- data/lib/i18n-inflector/api.rb +964 -0
- data/lib/i18n-inflector/api_strict.rb +519 -0
- data/lib/i18n-inflector/backend.rb +77 -40
- data/lib/i18n-inflector/errors.rb +56 -14
- data/lib/i18n-inflector/inflection_data.rb +133 -105
- data/lib/i18n-inflector/inflection_data_strict.rb +290 -0
- data/lib/i18n-inflector/inflector.rb +21 -660
- data/lib/i18n-inflector/lazy_enum.rb +120 -0
- data/lib/i18n-inflector/long_comments.rb +203 -14
- data/lib/i18n-inflector/version.rb +1 -1
- data/lib/i18n-inflector.rb +5 -3
- data/test/inflector_test.rb +334 -34
- data/test/test_helper.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +10 -6
- metadata.gz.sig +0 -0
- data/lib/i18n-inflector/util.rb +0 -67
@@ -4,8 +4,8 @@
|
|
4
4
|
# Copyright:: (c) 2011 by Paweł Wilk
|
5
5
|
# License:: This program is licensed under the terms of {file:LGPL GNU Lesser General Public License} or {file:COPYING Ruby License}.
|
6
6
|
#
|
7
|
-
# This file contains
|
8
|
-
#
|
7
|
+
# This file contains class that is used to keep
|
8
|
+
# inflection data.
|
9
9
|
|
10
10
|
# @abstract This namespace is shared with I18n subsystem.
|
11
11
|
module I18n
|
@@ -13,38 +13,50 @@ module I18n
|
|
13
13
|
|
14
14
|
# This class contains structures for keeping parsed translation data
|
15
15
|
# and basic operations for performing on them.
|
16
|
-
class InflectionData
|
16
|
+
class InflectionData < InflectionData_Strict
|
17
17
|
|
18
18
|
# Initializes internal structures.
|
19
|
+
#
|
20
|
+
# @param [Symbol,nil] locale the locale identifier for the object to be labeled with
|
19
21
|
def initialize(locale=nil)
|
20
|
-
|
21
|
-
@
|
22
|
-
@
|
23
|
-
@
|
24
|
-
@locale
|
22
|
+
@kinds = Hash.new(false)
|
23
|
+
@tokens = Hash.new(DUMMY_TOKEN)
|
24
|
+
@defaults = Hash.new
|
25
|
+
@lazy_tokens = LazyHashEnumerator.new(@tokens)
|
26
|
+
@locale = locale
|
25
27
|
end
|
26
28
|
|
27
|
-
#
|
28
|
-
attr_reader :locale
|
29
|
-
|
30
|
-
# Adds an alias (overwriting existing alias).
|
29
|
+
# Adds an alias (overwriting an existing alias).
|
31
30
|
#
|
32
|
-
# @param [Symbol] name the name of an alias
|
33
|
-
# @param [Symbol] target the target token for the given +alias+
|
34
31
|
# @return [Boolean] +true+ if everything went ok, +false+ otherwise
|
35
|
-
#
|
36
|
-
|
32
|
+
# (in case of bad or +nil+ names or non-existent targets)
|
33
|
+
# @overload add_alias(name, target)
|
34
|
+
# Adds an alias (overwriting an existing alias).
|
35
|
+
# @param [Symbol] name the name of an alias
|
36
|
+
# @param [Symbol] target the target token for the given +alias+
|
37
|
+
# @return [Boolean] +true+ if everything went ok, +false+ otherwise
|
38
|
+
# (in case of bad or +nil+ names or non-existent targets)
|
39
|
+
# @overload add_alias(name, target, kind)
|
40
|
+
# Adds an alias (overwriting an existing alias) if the given
|
41
|
+
# +kind+ matches the kind of the given target.
|
42
|
+
# @param [Symbol] name the name of an alias
|
43
|
+
# @param [Symbol] target the target token for the given +alias+
|
44
|
+
# @param [Symbol] kind the optional kind of a taget
|
45
|
+
# @return [Boolean] +true+ if everything went ok, +false+ otherwise
|
46
|
+
# (in case of bad or +nil+ names or non-existent targets)
|
47
|
+
def add_alias(name, target, kind=nil)
|
37
48
|
target = target.to_s
|
38
49
|
name = name.to_s
|
39
50
|
return false if (name.empty? || target.empty?)
|
51
|
+
kind = nil if kind.to_s.empty? unless kind.nil?
|
40
52
|
name = name.to_sym
|
41
53
|
target = target.to_sym
|
42
|
-
|
43
|
-
return false if kind.nil?
|
54
|
+
t_kind = get_kind(target)
|
55
|
+
return false if (t_kind.nil? || (!kind.nil? && t_kind != kind))
|
44
56
|
@tokens[name] = {}
|
45
|
-
@tokens[name][:kind]
|
46
|
-
@tokens[name][:target]
|
47
|
-
@tokens[name][:description]
|
57
|
+
@tokens[name][:kind] = kind
|
58
|
+
@tokens[name][:target] = target
|
59
|
+
@tokens[name][:description] = @tokens[target][:description]
|
48
60
|
true
|
49
61
|
end
|
50
62
|
|
@@ -53,8 +65,10 @@ module I18n
|
|
53
65
|
# @param [Symbol] token the name of a token to add
|
54
66
|
# @param [Symbol] kind the kind of a token
|
55
67
|
# @param [String] description the description of a token
|
56
|
-
# @return [
|
68
|
+
# @return [Boolean] +true+ if everything went ok, +false+ otherwise
|
69
|
+
# (in case of bad names or non-existent targets)
|
57
70
|
def add_token(token, kind, description)
|
71
|
+
return false if (token.to_s.empty? || kind.to_s.empty? || description.nil?)
|
58
72
|
token = token.to_sym
|
59
73
|
@tokens[token] = {}
|
60
74
|
@tokens[token][:kind] = kind.to_sym
|
@@ -62,16 +76,6 @@ module I18n
|
|
62
76
|
@kinds[kind] = true
|
63
77
|
end
|
64
78
|
|
65
|
-
# Sets the default token for a kind.
|
66
|
-
#
|
67
|
-
# @param [Symbol] kind the kind to which the default
|
68
|
-
# token should be assigned
|
69
|
-
# @param [Symbol] target the token to set
|
70
|
-
# @return [void]
|
71
|
-
def set_default_token(kind, target)
|
72
|
-
@defaults[kind.to_sym] = target.to_sym
|
73
|
-
end
|
74
|
-
|
75
79
|
# Tests if the token is a true token.
|
76
80
|
#
|
77
81
|
# @overload has_true_token?(token)
|
@@ -81,6 +85,7 @@ module I18n
|
|
81
85
|
# a token and not an alias, +false+ otherwise
|
82
86
|
# @overload has_true_token?(token, kind)
|
83
87
|
# Tests if the token is a true token.
|
88
|
+
# The kind will work as the expectation filter.
|
84
89
|
# @param [Symbol] token the identifier of a token
|
85
90
|
# @param [Symbol] kind the identifier of a kind
|
86
91
|
# @return [Boolean] +true+ if the given +token+ is
|
@@ -102,6 +107,7 @@ module I18n
|
|
102
107
|
# (which may be an alias) exists
|
103
108
|
# @overload has_token(token, kind)
|
104
109
|
# Tests if a token (or alias) is present.
|
110
|
+
# The kind will work as the expectation filter.
|
105
111
|
# @param [Symbol] token the identifier of a token
|
106
112
|
# @param [Symbol] kind the identifier of a kind
|
107
113
|
# @return [Boolean] +true+ if the given +token+
|
@@ -129,15 +135,16 @@ module I18n
|
|
129
135
|
@defaults.has_key?(kind)
|
130
136
|
end
|
131
137
|
|
132
|
-
# Tests if
|
138
|
+
# Tests if the given alias is really an alias.
|
133
139
|
#
|
134
140
|
# @overload has_alias?(alias_name)
|
135
|
-
# Tests if
|
141
|
+
# Tests if the given alias is really an alias.
|
136
142
|
# @param [Symbol] alias_name the identifier of an alias
|
137
143
|
# @return [Boolean] +true+ if the given alias is really an alias,
|
138
144
|
# +false+ otherwise
|
139
145
|
# @overload has_alias?(alias_name, kind)
|
140
|
-
# Tests if
|
146
|
+
# Tests if the given alias is really an alias.
|
147
|
+
# The kind will work as the expectation filter.
|
141
148
|
# @param [Symbol] alias_name the identifier of an alias
|
142
149
|
# @param [Symbol] kind the identifier of a kind
|
143
150
|
# @return [Boolean] +true+ if the given alias is really an alias
|
@@ -148,45 +155,49 @@ module I18n
|
|
148
155
|
kind.nil? ? true : o[:kind] == kind
|
149
156
|
end
|
150
157
|
|
151
|
-
# Reads
|
158
|
+
# Reads all the true tokens (not aliases).
|
152
159
|
#
|
153
160
|
# @return [Hash] the true tokens in a
|
154
161
|
# form of Hash (<tt>token => description</tt>)
|
155
|
-
# @overload get_true_tokens
|
156
|
-
# Reads
|
162
|
+
# @overload get_true_tokens
|
163
|
+
# Reads all the true tokens (not aliases).
|
157
164
|
# @return [Hash] the true tokens in a
|
158
165
|
# form of Hash (<tt>token => description</tt>)
|
159
166
|
# @overload get_true_tokens(kind)
|
160
|
-
# Reads
|
167
|
+
# Reads all the true tokens (not aliases) of the given +kind+.
|
161
168
|
# @param [Symbol] kind the identifier of a kind
|
162
|
-
# @return [Hash] the true tokens
|
169
|
+
# @return [Hash] the true tokens in a
|
163
170
|
# form of Hash (<tt>token => description</tt>)
|
164
171
|
def get_true_tokens(kind=nil)
|
165
|
-
|
166
|
-
|
167
|
-
|
172
|
+
t = @lazy_tokens
|
173
|
+
t = t.select { |token,data| data[:kind] == kind } unless kind.nil?
|
174
|
+
t.select { |token,data| data[:target].nil? }.
|
175
|
+
map { |token,data| data[:description] }.
|
176
|
+
to_h
|
168
177
|
end
|
169
178
|
|
170
|
-
# Reads
|
179
|
+
# Reads all the aliases.
|
171
180
|
#
|
172
181
|
# @return [Hash] the aliases in a
|
173
182
|
# form of Hash (<tt>alias => target</tt>)
|
174
|
-
# @overload get_aliases
|
175
|
-
# Reads
|
183
|
+
# @overload get_aliases
|
184
|
+
# Reads all the aliases.
|
176
185
|
# @return [Hash] the aliases in a
|
177
186
|
# form of Hash (<tt>alias => target</tt>)
|
178
187
|
# @overload get_aliases(kind)
|
179
|
-
# Reads the
|
188
|
+
# Reads all the aliases of the given +kind+.
|
180
189
|
# @param [Symbol] kind the identifier of a kind
|
181
|
-
# @return [Hash] the aliases
|
190
|
+
# @return [Hash] the aliases in a
|
182
191
|
# form of Hash (<tt>alias => target</tt>)
|
183
192
|
def get_aliases(kind=nil)
|
184
|
-
|
185
|
-
|
186
|
-
|
193
|
+
t = @lazy_tokens
|
194
|
+
t = t.select { |token,data| data[:kind] == kind } unless kind.nil?
|
195
|
+
t.reject { |token,data| data[:target].nil? }.
|
196
|
+
map { |token,data| data[:target] }.
|
197
|
+
to_h
|
187
198
|
end
|
188
199
|
|
189
|
-
# Reads
|
200
|
+
# Reads all the tokens in a way that it is possible to
|
190
201
|
# distinguish true tokens from aliases.
|
191
202
|
#
|
192
203
|
# @note True tokens have descriptions (String) and aliases
|
@@ -194,55 +205,85 @@ module I18n
|
|
194
205
|
# @return [Hash] the tokens in a
|
195
206
|
# form of Hash (<tt>token => description|target</tt>)
|
196
207
|
# @overload get_raw_tokens
|
197
|
-
# Reads
|
208
|
+
# Reads all the tokens in a way that it is possible to
|
209
|
+
# distinguish true tokens from aliases.
|
198
210
|
# @return [Hash] the tokens in a
|
199
211
|
# form of Hash (<tt>token => description|target</tt>)
|
200
212
|
# @overload get_raw_tokens(kind)
|
201
|
-
# Reads the
|
213
|
+
# Reads all the tokens of the given +kind+ in a way
|
214
|
+
# that it is possible to distinguish true tokens from aliases.
|
202
215
|
# @param [Symbol] kind the identifier of a kind
|
203
|
-
# @return [Hash] the tokens
|
216
|
+
# @return [Hash] the tokens in a
|
204
217
|
# form of Hash (<tt>token => description|target</tt>)
|
205
218
|
def get_raw_tokens(kind=nil)
|
206
|
-
|
219
|
+
t = @lazy_tokens
|
220
|
+
t = t.select { |token,data| data[:kind] == kind } unless kind.nil?
|
221
|
+
t.map { |token,data| data[:target] || data[:description] }.
|
222
|
+
to_h
|
207
223
|
end
|
208
224
|
|
209
|
-
# Reads
|
225
|
+
# Reads all the tokens (including aliases).
|
210
226
|
#
|
211
|
-
# @note Use {get_raw_tokens} if you want to distinguish
|
227
|
+
# @note Use {#get_raw_tokens} if you want to distinguish
|
212
228
|
# true tokens from aliases.
|
213
229
|
# @return [Hash] the tokens in a
|
214
230
|
# form of Hash (<tt>token => description</tt>)
|
215
|
-
# @overload
|
216
|
-
# Reads
|
231
|
+
# @overload get_tokens
|
232
|
+
# Reads all the tokens (including aliases).
|
217
233
|
# @return [Hash] the tokens in a
|
218
234
|
# form of Hash (<tt>token => description</tt>)
|
219
|
-
# @overload
|
220
|
-
# Reads
|
235
|
+
# @overload get_tokens(kind)
|
236
|
+
# Reads all the tokens (including aliases) of the
|
237
|
+
# given +kind+.
|
221
238
|
# @param [Symbol] kind the identifier of a kind
|
222
|
-
# @return [Hash] the tokens
|
239
|
+
# @return [Hash] the tokens in a
|
223
240
|
# form of Hash (<tt>token => description</tt>)
|
224
241
|
def get_tokens(kind=nil)
|
225
|
-
|
226
|
-
|
227
|
-
|
242
|
+
t = @lazy_tokens
|
243
|
+
t = t.select { |token,data| data[:kind] == kind } unless kind.nil?
|
244
|
+
t.map { |token,data| data[:description] }.
|
245
|
+
to_h
|
228
246
|
end
|
229
247
|
|
230
248
|
# Gets a target token for the alias.
|
231
249
|
#
|
232
|
-
# @param [Symbol] alias_name the identifier of an alias
|
233
250
|
# @return [Symbol,nil] the token that the given alias points to
|
234
251
|
# or +nil+ if it isn't really an alias
|
235
|
-
|
252
|
+
# @overload get_target_for_alias(alias_name)
|
253
|
+
# Gets a target token for the alias.
|
254
|
+
# @param [Symbol] alias_name the identifier of an alias
|
255
|
+
# @return [Symbol,nil] the token that the given alias points to
|
256
|
+
# or +nil+ if it isn't really an alias
|
257
|
+
# @overload get_target_for_alias(alias_name, kind)
|
258
|
+
# Gets a target token for the alias that's +kind+ is given.
|
259
|
+
# @param [Symbol] alias_name the identifier of an alias
|
260
|
+
# @param [Symbol] kind the identifier of a kind
|
261
|
+
# @return [Symbol,nil] the token that the given alias points to
|
262
|
+
# or +nil+ if it isn't really an alias
|
263
|
+
def get_target_for_alias(alias_name, kind=nil)
|
236
264
|
@tokens[alias_name][:target]
|
237
265
|
end
|
238
266
|
|
239
267
|
# Gets a kind of the given token or alias.
|
240
268
|
#
|
241
|
-
# @param [Symbol] token identifier of a token
|
242
269
|
# @return [Symbol,nil] the kind of the given +token+
|
243
270
|
# or +nil+ if the token is unknown
|
244
|
-
|
245
|
-
|
271
|
+
# @overload get_kind(token)
|
272
|
+
# Gets a kind of the given token or alias.
|
273
|
+
# @param [Symbol] token identifier of a token
|
274
|
+
# @return [Symbol,nil] the kind of the given +token+
|
275
|
+
# or +nil+ if the token is unknown
|
276
|
+
# @overload get_kind(token, kind)
|
277
|
+
# Gets a kind of the given token or alias.
|
278
|
+
# The kind will work as the expectation filter.
|
279
|
+
# @param [Symbol] token identifier of a token
|
280
|
+
# @param [Symbol] kind the identifier of a kind
|
281
|
+
# @return [Symbol,nil] the kind of the given +token+
|
282
|
+
# or +nil+ if the token is unknown
|
283
|
+
def get_kind(token, kind=nil)
|
284
|
+
k = @tokens[token][:kind]
|
285
|
+
return k if (kind.nil? || kind == k)
|
286
|
+
nil
|
246
287
|
end
|
247
288
|
|
248
289
|
# Gets a true token for the given identifier.
|
@@ -250,19 +291,21 @@ module I18n
|
|
250
291
|
# @note If the given +token+ is really an alias it will
|
251
292
|
# be resolved and the real token pointed by that alias
|
252
293
|
# will be returned.
|
294
|
+
# @return [Symbol,nil] the true token for the given +token+
|
295
|
+
# or +nil+
|
253
296
|
# @overload get_true_token(token)
|
254
|
-
# Gets a true token for the given token identifier.
|
297
|
+
# Gets a true token for the given +token+ identifier.
|
255
298
|
# @param [Symbol] token the identifier of a token
|
256
299
|
# @return [Symbol,nil] the true token for the given +token+
|
257
300
|
# or +nil+ if the token is unknown
|
258
301
|
# @overload get_true_token(token, kind)
|
259
|
-
# Gets a true token for the given token identifier and the
|
260
|
-
#
|
302
|
+
# Gets a true token for the given +token+ identifier and the
|
303
|
+
# given +kind+. The kind will work as the expectation filter.
|
261
304
|
# @param [Symbol] token the identifier of a token
|
262
305
|
# @param [Symbol] kind the identifier of a kind
|
263
306
|
# @return [Symbol,nil] the true token for the given +token+
|
264
|
-
# or +nil+ if the token is unknown or is not kind of the
|
265
|
-
# given kind
|
307
|
+
# or +nil+ if the token is unknown or is not a kind of the
|
308
|
+
# given +kind+
|
266
309
|
def get_true_token(token, kind=nil)
|
267
310
|
o = @tokens[token]
|
268
311
|
k = o[:kind]
|
@@ -289,38 +332,23 @@ module I18n
|
|
289
332
|
@defaults[kind]
|
290
333
|
end
|
291
334
|
|
292
|
-
# Gets a description of a token or alias.
|
293
|
-
#
|
335
|
+
# Gets a description of a token or an alias.
|
294
336
|
# @note If the token is really an alias it will resolve the alias first.
|
295
|
-
# @param [Symbol] token the identifier of a token
|
296
337
|
# @return [String,nil] the string containing description of the given
|
297
338
|
# token (which may be an alias) or +nil+ if the token is unknown
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
#
|
303
|
-
#
|
304
|
-
#
|
305
|
-
#
|
306
|
-
#
|
307
|
-
#
|
308
|
-
#
|
309
|
-
def
|
310
|
-
@
|
311
|
-
ttok = get_true_token(pointer)
|
312
|
-
return [kind, pointer] if ttok.nil?
|
313
|
-
set_default_token(kind, ttok)
|
314
|
-
end
|
315
|
-
return nil
|
316
|
-
end
|
317
|
-
|
318
|
-
# Test if the inflection data have no elements.
|
319
|
-
#
|
320
|
-
# @return [Boolean] +true+ if the inflection data
|
321
|
-
# have no elements
|
322
|
-
def empty?
|
323
|
-
@tokens.empty?
|
339
|
+
# @overload get_description(token)
|
340
|
+
# Gets a description of a token or an alias.
|
341
|
+
# @param [Symbol] token the identifier of a token
|
342
|
+
# @return [String,nil] the string containing description of the given
|
343
|
+
# token (which may be an alias) or +nil+ if the token is unknown
|
344
|
+
# @overload get_description(token, kind)
|
345
|
+
# Gets a description of a token or an alias of the given +kind+
|
346
|
+
# @param [Symbol] token the identifier of a token
|
347
|
+
# @param [Symbol] kind the identifier of a kind
|
348
|
+
# @return [String,nil] the string containing description of the given
|
349
|
+
# token (which may be an alias) or +nil+ if the token is unknown
|
350
|
+
def get_description(token, kind=nil)
|
351
|
+
@tokens[token][:description] if (kind.nil? || @tokens[token][:kind] == kind)
|
324
352
|
end
|
325
353
|
|
326
354
|
end # InflectionData
|
@@ -0,0 +1,290 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Author:: Paweł Wilk (mailto:pw@gnu.org)
|
4
|
+
# Copyright:: (c) 2011 by Paweł Wilk
|
5
|
+
# License:: This program is licensed under the terms of {file:LGPL GNU Lesser General Public License} or {file:COPYING Ruby License}.
|
6
|
+
#
|
7
|
+
# This file contains class that is used to keep
|
8
|
+
# inflection data for strict kinds.
|
9
|
+
|
10
|
+
# @abstract This namespace is shared with I18n subsystem.
|
11
|
+
module I18n
|
12
|
+
module Inflector
|
13
|
+
|
14
|
+
# This class contains structures for keeping parsed translation data
|
15
|
+
# and basic operations for strict kinds and tokens assigned to them.
|
16
|
+
# Methods in this class vary from methods from {I18n::Inflector::InflectionData}
|
17
|
+
# in a way that +kind+ argument is usually required, not optional, since
|
18
|
+
# managing the strict kinds requires a kind of any token to be always known.
|
19
|
+
class InflectionData_Strict
|
20
|
+
|
21
|
+
# This constant contains a dummy hash for an empty token. It makes
|
22
|
+
# chaining calls to internal data easier.
|
23
|
+
DUMMY_TOKEN = {:kind=>nil, :target=>nil, :description=>nil}.freeze
|
24
|
+
|
25
|
+
# This constant contains a dummy hash of hashes for tokens collection.
|
26
|
+
# It makes chaining calls to internal data easier.
|
27
|
+
DUMMY_TOKENS = Hash.new(DUMMY_TOKEN).freeze
|
28
|
+
|
29
|
+
# This constant contains a dummy iterator for hash of hashes.
|
30
|
+
# It makes chaining calls to internal data easier.
|
31
|
+
DUMMY_T_LAZY = LazyHashEnumerator.new(DUMMY_TOKENS).freeze
|
32
|
+
|
33
|
+
# This constant contains a dummy hash. It makes
|
34
|
+
# chaining calls to internal data easier.
|
35
|
+
DUMMY_HASH = Hash.new.freeze
|
36
|
+
|
37
|
+
# Locale that this database works for.
|
38
|
+
attr_reader :locale
|
39
|
+
|
40
|
+
# Initializes internal structures.
|
41
|
+
#
|
42
|
+
# @param [Symbol,nil] locale the locale identifier for
|
43
|
+
# the object to be labeled with
|
44
|
+
def initialize(locale=nil)
|
45
|
+
@tokens = Hash.new(DUMMY_TOKENS)
|
46
|
+
@lazy_tokens = Hash.new(DUMMY_T_LAZY)
|
47
|
+
@defaults = Hash.new
|
48
|
+
@locale = locale
|
49
|
+
end
|
50
|
+
|
51
|
+
# Adds an alias (overwriting existing alias).
|
52
|
+
#
|
53
|
+
# @param [Symbol] name the name of an alias
|
54
|
+
# @param [Symbol] target the target token for the created alias
|
55
|
+
# @param [Symbol] kind the identifier of a kind
|
56
|
+
# @return [Boolean] +true+ if everything went ok, +false+ otherwise
|
57
|
+
# (in case of bad names or non-existent targets)
|
58
|
+
def add_alias(name, target, kind)
|
59
|
+
return false if (name.nil? || target.nil? || kind.nil?)
|
60
|
+
return false if (name.to_s.empty? || target.to_s.empty? || kind.to_s.empty?)
|
61
|
+
name = name.to_sym
|
62
|
+
target = target.to_sym
|
63
|
+
kind = kind.to_sym
|
64
|
+
k = @tokens[kind]
|
65
|
+
return false unless k.has_key?(target)
|
66
|
+
token = k[name] = {}
|
67
|
+
token[:description] = k[target][:description]
|
68
|
+
token[:target] = target
|
69
|
+
true
|
70
|
+
end
|
71
|
+
|
72
|
+
# Adds a token (overwriting existing token).
|
73
|
+
#
|
74
|
+
# @param [Symbol] token the name of a token to add
|
75
|
+
# @param [Symbol] kind the identifier of a kind
|
76
|
+
# @param [String] description the description of a token
|
77
|
+
# @return [Boolean] +true+ if everything went ok, +false+ otherwise
|
78
|
+
# (in case of bad names)
|
79
|
+
def add_token(token, kind, description)
|
80
|
+
return false if (token.to_s.empty? || kind.to_s.empty? || description.nil?)
|
81
|
+
token = token.to_sym
|
82
|
+
kind = kind.to_sym
|
83
|
+
kind_tree = @tokens[kind]
|
84
|
+
if kind_tree.equal?(DUMMY_TOKENS)
|
85
|
+
kind_tree = @tokens[kind] = Hash.new(DUMMY_TOKEN)
|
86
|
+
@lazy_tokens[kind] = LazyHashEnumerator.new(kind_tree)
|
87
|
+
end
|
88
|
+
token = kind_tree[token] = {}
|
89
|
+
token[:description] = description.to_s
|
90
|
+
true
|
91
|
+
end
|
92
|
+
|
93
|
+
# Sets the default token for the given strict kind.
|
94
|
+
#
|
95
|
+
# @param [Symbol] kind the kind to which the default
|
96
|
+
# token should be assigned
|
97
|
+
# @param [Symbol] target the token to set
|
98
|
+
# @return [void]
|
99
|
+
def set_default_token(kind, target)
|
100
|
+
@defaults[kind.to_sym] = target.to_sym
|
101
|
+
end
|
102
|
+
|
103
|
+
# Tests if the given token of the given
|
104
|
+
# strict kind is a true token.
|
105
|
+
#
|
106
|
+
# @param [Symbol] token the identifier of a token
|
107
|
+
# @param [Symbol] kind the identifier of a kind
|
108
|
+
# @return [Boolean] +true+ if the given +token+ is
|
109
|
+
# a token and not an alias, and is a kind of
|
110
|
+
# the given kind, +false+ otherwise
|
111
|
+
def has_true_token?(token, kind)
|
112
|
+
@tokens[kind].has_key?(token) && @tokens[kind][token][:target].nil?
|
113
|
+
end
|
114
|
+
|
115
|
+
# Tests if the given token (or alias) of the
|
116
|
+
# given strict kind is present.
|
117
|
+
#
|
118
|
+
# @param [Symbol] token the identifier of a token
|
119
|
+
# @param [Symbol] kind the identifier of a kind
|
120
|
+
# @return [Boolean] +true+ if the given +token+
|
121
|
+
# (which may be an alias) exists and if kind of
|
122
|
+
# the given kind
|
123
|
+
def has_token?(token, kind)
|
124
|
+
@tokens[kind].has_key?(token)
|
125
|
+
end
|
126
|
+
|
127
|
+
# Tests if a strict kind exists.
|
128
|
+
#
|
129
|
+
# @param [Symbol] kind the identifier of a kind
|
130
|
+
# @return [Boolean] +true+ if the given +kind+ exists
|
131
|
+
def has_kind?(kind)
|
132
|
+
@tokens.has_key?(kind)
|
133
|
+
end
|
134
|
+
|
135
|
+
# Tests if the given strict kind has a default
|
136
|
+
# token assigned.
|
137
|
+
#
|
138
|
+
# @param [Symbol] kind the identifier of a kind
|
139
|
+
# @return [Boolean] +true+ if there is a default
|
140
|
+
# token of the given kind
|
141
|
+
def has_default_token?(kind)
|
142
|
+
@defaults.has_key?(kind)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Tests if the given alias of the given strict
|
146
|
+
# kind is really an alias.
|
147
|
+
#
|
148
|
+
# @param [Symbol] alias_name the identifier of an alias
|
149
|
+
# @param [Symbol] kind the identifier of a kind
|
150
|
+
# @return [Boolean] +true+ if the given alias is really an alias
|
151
|
+
# being a kind of the given kind, +false+ otherwise
|
152
|
+
def has_alias?(alias_name, kind)
|
153
|
+
not @tokens[kind][alias_name][:target].nil?
|
154
|
+
end
|
155
|
+
|
156
|
+
# Reads all the true tokens (not aliases) of the
|
157
|
+
# given strict kind.
|
158
|
+
#
|
159
|
+
# @param [Symbol] kind the identifier of a kind
|
160
|
+
# @return [Hash] the true tokens of the given kind in a
|
161
|
+
# form of Hash (<tt>token => description</tt>)
|
162
|
+
def get_true_tokens(kind)
|
163
|
+
@lazy_tokens[kind].
|
164
|
+
reject { |token,data| !data[:target].nil? }.
|
165
|
+
map { |token,data| data[:description] }.
|
166
|
+
to_h
|
167
|
+
end
|
168
|
+
|
169
|
+
# Reads all the aliases of the given strict kind.
|
170
|
+
#
|
171
|
+
# @param [Symbol] kind the identifier of a kind
|
172
|
+
# @return [Hash] the aliases of the given kind in a
|
173
|
+
# form of Hash (<tt>alias => target</tt>)
|
174
|
+
def get_aliases(kind)
|
175
|
+
@lazy_tokens[kind].
|
176
|
+
reject { |token,data| data[:target].nil? }.
|
177
|
+
map { |token,data| data[:target] }.
|
178
|
+
to_h
|
179
|
+
end
|
180
|
+
|
181
|
+
# Reads all the tokens of the given strict kind
|
182
|
+
# in a way that it is possible to
|
183
|
+
# distinguish true tokens from aliases.
|
184
|
+
#
|
185
|
+
# @note True tokens have descriptions (String) and aliases
|
186
|
+
# have targets (Symbol) assigned.
|
187
|
+
# @param [Symbol] kind the identifier of a kind
|
188
|
+
# @return [Hash] the tokens of the given kind in a
|
189
|
+
# form of Hash (<tt>token => description|target</tt>)
|
190
|
+
def get_raw_tokens(kind)
|
191
|
+
@lazy_tokens[kind].
|
192
|
+
map { |token,data| data[:target] || data[:description] }.
|
193
|
+
to_h
|
194
|
+
end
|
195
|
+
|
196
|
+
# Reads all the tokens (including aliases) of the given
|
197
|
+
# strict kind.
|
198
|
+
#
|
199
|
+
# @note Use {#get_raw_tokens} if you want to distinguish
|
200
|
+
# true tokens from aliases.
|
201
|
+
# @param [Symbol] kind the identifier of a kind
|
202
|
+
# @return [Hash] the tokens of the given kind in a
|
203
|
+
# form of Hash (<tt>token => description</tt>)
|
204
|
+
def get_tokens(kind)
|
205
|
+
@lazy_tokens[kind].map{ |token,data| data[:description] }.to_h
|
206
|
+
end
|
207
|
+
|
208
|
+
# Gets a target token for the given alias of a strict kind.
|
209
|
+
#
|
210
|
+
# @param [Symbol] alias_name the identifier of an alias
|
211
|
+
# @param [Symbol] kind the identifier of a kind
|
212
|
+
# @return [Symbol,nil] the token that the given alias points to
|
213
|
+
# or +nil+ if it isn't really an alias
|
214
|
+
def get_target_for_alias(alias_name, kind)
|
215
|
+
@tokens[kind][alias_name][:target]
|
216
|
+
end
|
217
|
+
|
218
|
+
# Gets a strict kind of the given token or alias.
|
219
|
+
#
|
220
|
+
# @note This method may be concidered dummy since there is a
|
221
|
+
# need to give the inflection kind, but it's here in order
|
222
|
+
# to preserve compatibility with the same method from
|
223
|
+
# {I18n::Inflector::InflectionData} which guesses the kind.
|
224
|
+
# @param [Symbol] token identifier of a token
|
225
|
+
# @param [Symbol] kind the identifier of a kind (expectations filter)
|
226
|
+
# @return [Symbol,nil] the kind of the given +token+
|
227
|
+
# or +nil+ if the token is unknown or is not of the given kind
|
228
|
+
def get_kind(token, kind)
|
229
|
+
@tokens[kind].has_key?(token) ? kind : nil
|
230
|
+
end
|
231
|
+
|
232
|
+
# Gets a true token (of the given strict kind) for the given
|
233
|
+
# identifier.
|
234
|
+
#
|
235
|
+
# @note If the given +token+ is really an alias it will
|
236
|
+
# be resolved and the real token pointed by that alias
|
237
|
+
# will be returned.
|
238
|
+
# @param [Symbol] token the identifier of a token
|
239
|
+
# @param [Symbol] kind the identifier of a kind
|
240
|
+
# @return [Symbol,nil] the true token for the given +token+
|
241
|
+
# or +nil+ if the token is unknown or is not a kind of the
|
242
|
+
# given +kind+
|
243
|
+
def get_true_token(token, kind)
|
244
|
+
o = @tokens[kind]
|
245
|
+
return nil unless o.has_key?(token)
|
246
|
+
o = o[token]
|
247
|
+
o[:target].nil? ? token : o[:target]
|
248
|
+
end
|
249
|
+
|
250
|
+
# Gets all known strict kinds.
|
251
|
+
#
|
252
|
+
# @return [Array<Symbol>] an array containing all the known strict
|
253
|
+
# kinds
|
254
|
+
def get_kinds
|
255
|
+
@tokens.keys
|
256
|
+
end
|
257
|
+
|
258
|
+
# Reads the default token of a strict kind.
|
259
|
+
#
|
260
|
+
# @note It will always return true token (not an alias).
|
261
|
+
# @param [Symbol] kind the identifier of a kind
|
262
|
+
# @return [Symbol,nil] the default token of the given +kind+
|
263
|
+
# or +nil+ if there is no default token set
|
264
|
+
def get_default_token(kind)
|
265
|
+
@defaults[kind]
|
266
|
+
end
|
267
|
+
|
268
|
+
# Gets a description of a token or alias belonging to a strict kind.
|
269
|
+
#
|
270
|
+
# @note If the token is really an alias it will resolve the alias first.
|
271
|
+
# @param [Symbol] token the identifier of a token
|
272
|
+
# @param [Symbol] kind the identifier of a kind
|
273
|
+
# @return [String,nil] the string containing description of the given
|
274
|
+
# token (which may be an alias) or +nil+ if the token is unknown
|
275
|
+
def get_description(token, kind)
|
276
|
+
@tokens[kind][token][:description]
|
277
|
+
end
|
278
|
+
|
279
|
+
# Test if the inflection data have no elements.
|
280
|
+
#
|
281
|
+
# @return [Boolean] +true+ if the inflection data
|
282
|
+
# have no elements
|
283
|
+
def empty?
|
284
|
+
@tokens.empty?
|
285
|
+
end
|
286
|
+
|
287
|
+
end # InflectionData_Strict
|
288
|
+
|
289
|
+
end
|
290
|
+
end
|