i18n-inflector-3 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,671 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Author:: Paweł Wilk (mailto:pw@gnu.org)
5
+ # Copyright:: (c) 2011,2012,2013 by Paweł Wilk
6
+ # License:: This program is licensed under the terms of {file:docs/LGPL GNU Lesser General Public License} or {file:docs/COPYING Ruby License}.
7
+ #
8
+ # This file contains I18n::Inflector::API_Strict class,
9
+ # which handles public API for managing inflection data
10
+ # for named patterns (strict kinds).
11
+
12
+ module I18n
13
+ module Inflector
14
+ # This class contains common operations
15
+ # that can be performed on inflection data describing
16
+ # strict kinds and tokens assigned to them (used in named
17
+ # patterns). It is used by the regular {I18n::Inflector::API API}
18
+ # and present there as {I18n::Inflector::API#strict strict}
19
+ # instance attribute.
20
+ #
21
+ # It operates on the database containing instances
22
+ # of {I18n::Inflector::InflectionData_Strict} indexed by locale
23
+ # names and has methods to access the inflection data in an easy way.
24
+ # It can operate on a database and options passed to initializer;
25
+ # if they aren't passet it will create them.
26
+ #
27
+ # ==== Usage
28
+ # You can access the instance of this class attached to
29
+ # default I18n backend by calling:
30
+ # I18n.backend.inflector.strict
31
+ # or in a short form:
32
+ # I18n.inflector.strict
33
+ #
34
+ # In most cases using the regular {I18n::Inflector::API} instance
35
+ # may be sufficient to operate on inflection data,
36
+ # because the regular API (instantiated as <tt>I18n.inflector</tt>)
37
+ # is aware of strict kinds and can pass calls from +API_Strict+
38
+ # object if the +kind+ argument given in a method call
39
+ # contains the +@+ symbol.
40
+ #
41
+ # For an instance connected to default I18n backend
42
+ # the object containing inflection options is shared
43
+ # with the regular API.
44
+ #
45
+ # @api public
46
+ class API_Strict
47
+ # Initilizes inflector by connecting to internal databases
48
+ # used for storing inflection data and options.
49
+ #
50
+ # @api public
51
+ # @note If any given option is +nil+ then a proper object will be created.
52
+ # If it's given, then it will be referenced, not copied.
53
+ # @param [Hash,nil] idb the strict inflections databases indexed by locale
54
+ # @param [I18n::Inflector::InflectionOptions,nil] options the inflection options
55
+ def initialize(idb = nil, options = nil)
56
+ @idb = idb.nil? ? {} : idb
57
+ @options = options.nil? ? I18n::Inflector::InflectionOptions.new : options
58
+ @lazy_locales = LazyHashEnumerator.for(@idb)
59
+ @inflected_locales_cache = {}
60
+ end
61
+
62
+ # Creates an empty strict inflections database for the specified locale.
63
+ #
64
+ # @api public
65
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
66
+ # @param [Symbol] locale the locale for which the inflections database
67
+ # should be created
68
+ # @return [I18n::Inflector::InflectionData_Strict] the new object for keeping
69
+ # inflection data
70
+ def new_database(locale)
71
+ locale = prep_locale(locale)
72
+ @inflected_locales_cache.clear
73
+ @idb[locale] = I18n::Inflector::InflectionData_Strict.new(locale)
74
+ end
75
+
76
+ # Attaches {I18n::Inflector::InflectionData_Strict} instance to the
77
+ # current object.
78
+ #
79
+ # @api public
80
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
81
+ # @note It doesn't create copy of inflection database, it registers the given object.
82
+ # @param [I18n::Inflector::InflectionData_Strict] idb inflection data to add
83
+ # @return [I18n::Inflector::InflectionData_Strict] the given +idb+ or +nil+ if something
84
+ # went wrong (e.g. +nil+ was given as an argument)
85
+ def add_database(db)
86
+ return nil if db.nil?
87
+
88
+ locale = prep_locale(db.locale)
89
+ delete_database(locale)
90
+ @inflected_locales_cache.clear
91
+ @idb[locale] = db
92
+ end
93
+
94
+ # Deletes a strict inflections database for the specified locale.
95
+ #
96
+ # @api public
97
+ # @note It detaches the database from {I18n::Inflector::API_Strict} instance.
98
+ # Other objects referring to it directly may still use it.
99
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
100
+ # @param [Symbol] locale the locale for which the inflections database is to be deleted.
101
+ # @return [void]
102
+ def delete_database(locale)
103
+ locale = prep_locale(locale)
104
+ return nil if @idb[locale].nil?
105
+
106
+ @inflected_locales_cache.clear
107
+ @idb[locale] = nil
108
+ end
109
+
110
+ # Checks if the given locale was configured to support strict inflection.
111
+ #
112
+ # @api public
113
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
114
+ # @return [Boolean] +true+ if a locale supports inflection
115
+ # @overload inflected_locale?
116
+ # Checks if the current locale was configured to support inflection.
117
+ # @return [Boolean] +true+ if the current locale supports inflection
118
+ # @overload inflected_locale?(locale)
119
+ # Checks if the given locale was configured to support inflection.
120
+ # @param [Symbol] locale the locale to test
121
+ # @return [Boolean] +true+ if the given locale supports inflection
122
+ def inflected_locale?(locale = nil)
123
+ !@idb[prep_locale(locale)].nil?
124
+ rescue StandardError
125
+ false
126
+ end
127
+ alias_method :locale?, :inflected_locale?
128
+ alias_method :locale_supported?, :inflected_locale?
129
+
130
+ # Iterates through locales which have configured strict inflection support.
131
+ #
132
+ # @api public
133
+ # @return [LazyArrayEnumerator] the lazy enumerator
134
+ # @yield [locale] optional block in which each kind will be yielded
135
+ # @yieldparam [Symbol] locale the inflected locale identifier
136
+ # @yieldreturn [LazyArrayEnumerator] the lazy enumerator
137
+ # @overload each_inflected_locale
138
+ # Iterates through locales which have configured inflection support.
139
+ # @return [LazyArrayEnumerator] the lazy enumerator
140
+ # @overload each_inflected_locale(kind)
141
+ # Iterates through locales which have configured inflection support for the given +kind+.
142
+ # @param [Symbol] kind the identifier of a kind
143
+ # @return [LazyArrayEnumerator] the lazy enumerator
144
+ def each_inflected_locale(kind = nil, &)
145
+ kind = kind.to_s.empty? ? nil : kind.to_sym
146
+ i = @lazy_locales.reject { |_lang, data| data.empty? }
147
+ i = i.select { |_lang, data| data.has_kind?(kind) } unless kind.nil?
148
+ i.each_key(&)
149
+ end
150
+ alias_method :each_locale, :each_inflected_locale
151
+ alias_method :each_supported_locale, :each_inflected_locale
152
+
153
+ # Gets locales which have configured strict inflection support.
154
+ #
155
+ # @api public
156
+ # @return [Array<Symbol>] the array containing locales that support inflection
157
+ # @overload inflected_locales
158
+ # Gets locales which have configured inflection support.
159
+ # @return [Array<Symbol>] the array containing locales that support inflection
160
+ # @overload inflected_locales(kind)
161
+ # Gets locales which have configured inflection support for the given +kind+.
162
+ # @param [Symbol] kind the identifier of a kind
163
+ # @return [Array<Symbol>] the array containing locales that support inflection
164
+ def inflected_locales(kind = nil)
165
+ kind = kind.to_s.empty? ? nil : kind.to_sym
166
+ r = (@inflected_locales_cache[kind] ||= each_inflected_locale(kind).to_a)
167
+ r.nil? ? r : r.dup
168
+ end
169
+ alias_method :locales, :inflected_locales
170
+ alias_method :supported_locales, :inflected_locales
171
+
172
+ # Iterates through known strict inflection kinds.
173
+ #
174
+ # @api public
175
+ # @return [LazyArrayEnumerator] the lazy enumerator
176
+ # @yield [kind] optional block in which each kind will be yielded
177
+ # @yieldparam [Symbol] kind the inflection kind
178
+ # @yieldreturn [LazyArrayEnumerator] the lazy enumerator
179
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
180
+ # @overload kinds
181
+ # Iterates through known inflection kinds for the current locale.
182
+ # @return [LazyArrayEnumerator] the lazy enumerator
183
+ # @overload kinds(locale)
184
+ # Iterates through known inflection kinds for the given +locale+.
185
+ # @param [Symbol] locale the locale for which kinds should be listed
186
+ # @return [LazyArrayEnumerator] the lazy enumerator
187
+ def each_kind(locale = nil, &)
188
+ data_safe(locale).each_kind(&)
189
+ end
190
+ alias_method :each_inflection_kind, :each_kind
191
+
192
+ # Gets known strict inflection kinds.
193
+ #
194
+ # @api public
195
+ # @return [Array<Symbol>] the array containing known inflection kinds
196
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
197
+ # @overload kinds
198
+ # Gets known inflection kinds for the current locale.
199
+ # @return [Array<Symbol>] the array containing known inflection kinds
200
+ # @overload kinds(locale)
201
+ # Gets known inflection kinds for the given +locale+.
202
+ # @param [Symbol] locale the locale for which kinds should be listed
203
+ # @return [Array<Symbol>] the array containing known inflection kinds
204
+ def kinds(locale = nil)
205
+ each_kind(locale).to_a
206
+ end
207
+ alias_method :inflection_kinds, :kinds
208
+
209
+ # Tests if a strict kind exists.
210
+ #
211
+ # @api public
212
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
213
+ # @return [Boolean] +true+ if the given +kind+ exists, +false+ otherwise
214
+ # @overload has_kind?(kind)
215
+ # Tests if a strict kind exists for the current locale.
216
+ # @param [Symbol] kind the identifier of a kind
217
+ # @return [Boolean] +true+ if the given +kind+ exists, +false+ otherwise
218
+ # @overload has_kind?(kind, locale)
219
+ # Tests if a strict kind exists for the given +locale+.
220
+ # @param [Symbol,String] kind the identifier of a kind
221
+ # @param [Symbol] locale the locale identifier
222
+ # @return [Boolean] +true+ if the given +kind+ exists, +false+ otherwise
223
+ def has_kind?(kind, locale = nil)
224
+ return false if kind.nil? || kind.to_s.empty?
225
+
226
+ data_safe(locale).has_kind?(kind.to_sym)
227
+ end
228
+
229
+ # Reads default token of the given strict +kind+.
230
+ #
231
+ # @api public
232
+ # @return [Symbol,nil] the default token or +nil+
233
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
234
+ # @overload default_token(kind)
235
+ # This method reads default token of the given +kind+ and the current locale.
236
+ # @param [Symbol,String] kind the kind of tokens
237
+ # @return [Symbol,nil] the default token or +nil+ if
238
+ # there is no default token
239
+ # @overload default_token(kind, locale)
240
+ # This method reads default token of the given +kind+ and the given +locale+.
241
+ # @param [Symbol,String] kind the kind of tokens
242
+ # @param [Symbol] locale the locale to use
243
+ # @return [Symbol,nil] the default token or +nil+ if
244
+ # there is no default token
245
+ def default_token(kind, locale = nil)
246
+ return nil if kind.nil? || kind.to_s.empty?
247
+
248
+ data_safe(locale).get_default_token(kind.to_sym)
249
+ end
250
+
251
+ # Checks if the given +token+ belonging to a strict kind is an alias.
252
+ #
253
+ # @api public
254
+ # @return [Boolean] +true+ if the given +token+ is an alias, +false+ otherwise
255
+ # @raise [I18n::InvalidLocale] if the given +locale+ is invalid
256
+ # @raise [ArgumentError] if the count of arguments is invalid
257
+ # @overload has_alias?(token, kind)
258
+ # Uses the current locale and the given +kind+ to check if the given +token+ is an alias.
259
+ # @param [Symbol,String] token name of the checked token
260
+ # @param [Symbol,String] kind the kind of the given token
261
+ # @return [Boolean] +true+ if the given +token+ is an alias, +false+ otherwise
262
+ # @overload has_alias?(token, kind, locale)
263
+ # Uses the given +locale+ and +kind+ to check if the given +token+ is an alias.
264
+ # @param [Symbol,String] token name of the checked token
265
+ # @param [Symbol,String] kind the kind of the given token
266
+ # @param [Symbol] locale the locale to use
267
+ # @return [Boolean] +true+ if the given +token+ is an alias, +false+ otherwise
268
+ def has_alias?(*args)
269
+ token, kind, locale = tkl_args(args)
270
+ return false if token.nil? || kind.nil?
271
+
272
+ data_safe(locale).has_alias?(token, kind)
273
+ end
274
+ alias_method :token_is_alias?, :has_alias?
275
+
276
+ # Checks if the given +token+ belonging to a strict kind is a true token (not alias).
277
+ #
278
+ # @api public
279
+ # @return [Boolean] +true+ if the given +token+ is a true token, +false+ otherwise
280
+ # @raise [I18n::InvalidLocale] if the given +locale+ is invalid
281
+ # @raise [ArgumentError] if the count of arguments is invalid
282
+ # @overload has_true_token?(token, kind)
283
+ # Uses the current locale and the given +kind+ to check if the given +token+ is a true token.
284
+ # @param [Symbol,String] token name of the checked token
285
+ # @param [Symbol,String] kind the kind of the given token
286
+ # @return [Boolean] +true+ if the given +token+ is a true token, +false+ otherwise
287
+ # @overload has_true_token?(token, kind, locale)
288
+ # Uses the given +locale+ and +kind+ to check if the given +token+ is a true token.
289
+ # @param [Symbol,String] token name of the checked token
290
+ # @param [Symbol,String] kind the kind of the given token
291
+ # @param [Symbol] locale the locale to use
292
+ # @return [Boolean] +true+ if the given +token+ is a true token, +false+ otherwise
293
+ def has_true_token?(*args)
294
+ token, kind, locale = tkl_args(args)
295
+ return false if token.nil? || kind.nil?
296
+
297
+ data_safe(locale).has_true_token?(token, kind)
298
+ end
299
+ alias_method :token_is_true?, :has_true_token?
300
+
301
+ # Checks if the given +token+ belonging to a strict kind exists. It may be an alias or a true token.
302
+ #
303
+ # @api public
304
+ # @return [Boolean] +true+ if the given +token+ exists, +false+ otherwise
305
+ # @raise [I18n::InvalidLocale] if the given +locale+ is invalid
306
+ # @raise [ArgumentError] if the count of arguments is invalid
307
+ # @overload has_token?(token, kind)
308
+ # Uses the current locale and the given kind +kind+ to check if the given +token+ exists.
309
+ # @param [Symbol,String] token name of the checked token
310
+ # @param [Symbol,String] kind the kind of the given token
311
+ # @return [Boolean] +true+ if the given +token+ exists, +false+ otherwise
312
+ # @overload has_token?(token, kind, locale)
313
+ # Uses the given +locale+ and +kind+ to check if the given +token+ exists.
314
+ # @param [Symbol,String] token name of the checked token
315
+ # @param [Symbol,String] kind the kind of the given token
316
+ # @param [Symbol] locale the locale to use
317
+ # @return [Boolean] +true+ if the given +token+ exists, +false+ otherwise
318
+ def has_token?(*args)
319
+ token, kind, locale = tkl_args(args)
320
+ return false if token.nil? || kind.nil?
321
+
322
+ data_safe(locale).has_token?(token, kind)
323
+ end
324
+ alias_method :token_exists?, :has_token?
325
+
326
+ # Gets true token for the given +token+ belonging to
327
+ # a strict kind. If the token is an alias it will be resolved
328
+ # and a true token (target) will be returned.
329
+ #
330
+ # @api public
331
+ # @return [Symbol,nil] the true token or +nil+
332
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
333
+ # @overload true_token(token, kind)
334
+ # Uses the current +locale+ and the given +kind+ to get a real token
335
+ # for the given +token+. If the token is an alias it will be resolved
336
+ # and a true token (target) will be returned.
337
+ # @param [Symbol,String] token the identifier of the checked token
338
+ # @param [Symbol,String] kind the identifier of a kind
339
+ # @return [Symbol,nil] the true token or +nil+
340
+ # @overload true_token(token, kind, locale)
341
+ # Uses the given +locale+ and +kind+ to get a real token
342
+ # for the given +token+. If the token is an alias it will be resolved
343
+ # and a true token (target) will be returned.
344
+ # @param [Symbol,String] token the identifier of the checked token
345
+ # @param [Symbol,String] kind the identifier of a kind
346
+ # @param [Symbol] locale the locale to use
347
+ # @return [Symbol,nil] the true token or +nil+
348
+ def true_token(*args)
349
+ token, kind, locale = tkl_args(args)
350
+ return nil if token.nil? || kind.nil?
351
+
352
+ data_safe(locale).get_true_token(token, kind)
353
+ end
354
+ alias_method :resolve_alias, :true_token
355
+
356
+ # Gets a kind of the given +token+ (which may be an alias) belonging to a strict kind.
357
+ #
358
+ # @api public
359
+ # @return [Symbol,nil] the kind of the given +token+ or +nil+
360
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
361
+ # @overload kind(token, kind)
362
+ # Uses current locale and the given +kind+ to get a kind of
363
+ # the given +token+ (which may be an alias).
364
+ # @param [Symbol,String] token name of the token or alias
365
+ # @param [Symbol,String] kind the identifier of a kind (expectations filter)
366
+ # @return [Symbol,nil] the kind of the given +token+ or +nil+
367
+ # @overload kind(token, kind, locale)
368
+ # Uses the given +locale+ to get a kind of the given +token+ (which may be an alias).
369
+ # @param [Symbol,String] token name of the token or alias
370
+ # @param [Symbol,String] kind the identifier of a kind (expectations filter)
371
+ # @param [Symbol] locale the locale to use
372
+ # @return [Symbol,nil] the kind of the given +token+ or +nil+
373
+ def kind(token, kind = nil, locale = nil)
374
+ return nil if token.nil? || kind.nil? || token.to_s.empty? || kind.to_s.empty?
375
+
376
+ data_safe(locale).get_kind(token.to_sym, kind.to_sym)
377
+ end
378
+
379
+ # Iterates through available inflection tokens belonging to a strict kind and their descriptions.
380
+ #
381
+ # @api public
382
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
383
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
384
+ # @yield [token, description] optional block in which each token will be yielded
385
+ # @yieldparam [Symbol] token a token
386
+ # @yieldparam [String] description a description string for a token
387
+ # @yieldreturn [LazyHashEnumerator] the lazy enumerator
388
+ # @note You cannot deduce where aliases are pointing to, since the information
389
+ # about a target is replaced by the description. To get targets use the
390
+ # {#raw_tokens} method. To simply list aliases and their targets use
391
+ # the {#aliases} method.
392
+ # @overload each_token(kind)
393
+ # Iterates through available inflection tokens and their descriptions for some +kind+ and
394
+ # the current locale.
395
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
396
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
397
+ # @overload each_token(kind, locale)
398
+ # Iterates through available inflection tokens and their descriptions of the given
399
+ # +kind+ and +locale+.
400
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
401
+ # @param [Symbol] locale the locale to use
402
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
403
+ def each_token(kind = nil, locale = nil, &)
404
+ kind = kind.to_s.empty? ? nil : kind.to_sym
405
+ data_safe(locale).each_token(kind, &)
406
+ end
407
+
408
+ # Gets available inflection tokens belonging to a strict kind and their descriptions.
409
+ #
410
+ # @api public
411
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
412
+ # @return [Hash] the hash containing available inflection tokens and descriptions
413
+ # @note You cannot deduce where aliases are pointing to, since the information
414
+ # about a target is replaced by the description. To get targets use the
415
+ # {#raw_tokens} method. To simply list aliases and their targets use
416
+ # the {#aliases} method.
417
+ # @overload tokens(kind)
418
+ # Gets available inflection tokens and their descriptions for some +kind+ and
419
+ # the current locale.
420
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
421
+ # @return [Hash] the hash containing available inflection tokens (including
422
+ # aliases) as keys and their descriptions as values
423
+ # @overload tokens(kind, locale)
424
+ # Gets available inflection tokens and their descriptions of the given
425
+ # +kind+ and +locale+.
426
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
427
+ # @param [Symbol] locale the locale to use
428
+ # @return [Hash] the hash containing available inflection tokens (including
429
+ # aliases) as keys and their descriptions as values
430
+ def tokens(kind = nil, locale = nil)
431
+ each_token(kind, locale).to_h
432
+ end
433
+
434
+ # Iterates through available inflection tokens belonging to a strict kind and their values.
435
+ #
436
+ # @api public
437
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description|target</tt>)
438
+ # @yield [token, value] optional block in which each token will be yielded
439
+ # @yieldparam [Symbol] token a token
440
+ # @yieldparam [Symbol, String] value a description string for a token or a target (if alias)
441
+ # @yieldreturn [LazyHashEnumerator] the lazy enumerator
442
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
443
+ # @note You may deduce whether the returned values are aliases or true tokens
444
+ # by testing if a value is a kind of Symbol or a String.
445
+ # @overload each_token_raw(kind)
446
+ # Iterates through available inflection tokens and their values of the given +kind+ and
447
+ # the current locale.
448
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
449
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description|target</tt>)
450
+ # @overload each_token_raw(kind, locale)
451
+ # Iterates through available inflection tokens (and their values) of the given +kind+ and +locale+.
452
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
453
+ # @param [Symbol] locale the locale to use
454
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description|target</tt>)
455
+ def each_token_raw(kind = nil, locale = nil, &)
456
+ kind = kind.to_s.empty? ? nil : kind.to_sym
457
+ data_safe(locale).each_raw_token(kind, &)
458
+ end
459
+ alias_method :each_raw_token, :each_token_raw
460
+
461
+ # Gets available inflection tokens belonging to a strict kind and their values.
462
+ #
463
+ # @api public
464
+ # @return [Hash] the hash containing available inflection tokens and descriptions (or alias pointers)
465
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
466
+ # @note You may deduce whether the returned values are aliases or true tokens
467
+ # by testing if a value is a kind of Symbol or a String.
468
+ # @overload tokens_raw(kind)
469
+ # Gets available inflection tokens and their values of the given +kind+ and
470
+ # the current locale.
471
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
472
+ # @return [Hash] the hash containing available inflection tokens as keys
473
+ # and their descriptions as values; in case of aliases the returned
474
+ # values are Symbols
475
+ # @overload tokens_raw(kind, locale)
476
+ # Gets available inflection tokens (and their values) of the given +kind+ and +locale+.
477
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
478
+ # @param [Symbol] locale the locale to use
479
+ # @return [Hash] the hash containing available inflection tokens as keys
480
+ # and their descriptions as values. In case of aliases the returned
481
+ # values are Symbols
482
+ def tokens_raw(kind = nil, locale = nil)
483
+ each_token_raw(kind, locale).to_h
484
+ end
485
+ alias_method :raw_tokens, :tokens_raw
486
+
487
+ # Iterates through inflection tokens belonging to a strict kind and their values.
488
+ #
489
+ # @api public
490
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
491
+ # @yield [token, description] optional block in which each token will be yielded
492
+ # @yieldparam [Symbol] token a token
493
+ # @yieldparam [String] description a description string for a token
494
+ # @yieldreturn [LazyHashEnumerator] the lazy enumerator
495
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
496
+ # @note It returns only true tokens, not aliases.
497
+ # @overload each_token_true(kind)
498
+ # Iterates through true inflection tokens (and their values) of the given +kind+ and
499
+ # the current locale.
500
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
501
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
502
+ # @overload each_token_true(kind, locale)
503
+ # Iterates through true inflection tokens (and their values) of the given +kind+ and +locale+.
504
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
505
+ # @param [Symbol] locale the locale to use
506
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
507
+ def each_token_true(kind = nil, locale = nil, &)
508
+ kind = kind.to_s.empty? ? nil : kind.to_sym
509
+ data_safe(locale).each_true_token(kind, &)
510
+ end
511
+ alias_method :each_true_token, :each_token_true
512
+
513
+ # Gets true inflection tokens belonging to a strict kind and their values.
514
+ #
515
+ # @api public
516
+ # @return [Hash] the hash containing available inflection tokens and descriptions
517
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
518
+ # @note It returns only true tokens, not aliases.
519
+ # @overload tokens_true(kind)
520
+ # Gets true inflection tokens (and their values) of the given +kind+ and
521
+ # the current locale.
522
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
523
+ # @return [Hash] the hash containing available inflection tokens as keys
524
+ # and their descriptions as values
525
+ # @overload tokens_true(kind, locale)
526
+ # Gets true inflection tokens (and their values) of the given +kind+ and +locale+.
527
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
528
+ # @param [Symbol] locale the locale to use
529
+ # @return [Hash] the hash containing available inflection tokens as keys
530
+ # and their descriptions as values
531
+ def tokens_true(kind = nil, locale = nil)
532
+ each_token_true(kind, locale).to_h
533
+ end
534
+ alias_method :true_tokens, :tokens_true
535
+
536
+ # Iterates through inflection aliases belonging to a strict kind and their pointers.
537
+ #
538
+ # @api public
539
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
540
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => target</tt>)
541
+ # @yield [alias, target] optional block in which each alias will be yielded
542
+ # @yieldparam [Symbol] alias an alias
543
+ # @yieldparam [Symbol] target a name of the target token
544
+ # @yieldreturn [LazyHashEnumerator] the lazy enumerator
545
+ # @overload each_alias(kind)
546
+ # Iterates through inflection aliases (and their pointers) of the given +kind+ and the current locale.
547
+ # @param [Symbol,String] kind the kind of aliases to get
548
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => target</tt>)
549
+ # @overload each_alias(kind, locale)
550
+ # Iterates through inflection aliases (and their pointers) of the given +kind+ and +locale+.
551
+ # @param [Symbol,String] kind the kind of aliases to get
552
+ # @param [Symbol] locale the locale to use
553
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => target</tt>)
554
+ def each_alias(kind = nil, locale = nil, &)
555
+ kind = kind.to_s.empty? ? nil : kind.to_sym
556
+ data_safe(locale).each_alias(kind, &)
557
+ end
558
+
559
+ # Gets inflection aliases belonging to a strict kind and their pointers.
560
+ #
561
+ # @api public
562
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
563
+ # @return [Hash] the Hash containing available inflection aliases (<tt>alias => target</tt>)
564
+ # @overload aliases(kind)
565
+ # Gets inflection aliases (and their pointers) of the given +kind+ and the current locale.
566
+ # @param [Symbol,String] kind the kind of aliases to get
567
+ # @return [Hash] the Hash containing available inflection aliases
568
+ # @overload aliases(kind, locale)
569
+ # Gets inflection aliases (and their pointers) of the given +kind+ and +locale+.
570
+ # @param [Symbol,String] kind the kind of aliases to get
571
+ # @param [Symbol] locale the locale to use
572
+ # @return [Hash] the Hash containing available inflection aliases
573
+ def aliases(kind = nil, locale = nil)
574
+ each_alias(kind, locale).to_h
575
+ end
576
+
577
+ # Gets the description of the given inflection token belonging to a strict kind.
578
+ #
579
+ # @api public
580
+ # @note If the given +token+ is really an alias it
581
+ # returns the description of the true token that
582
+ # it points to.
583
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
584
+ # @return [String,nil] the descriptive string or +nil+
585
+ # @overload token_description(token, kind)
586
+ # Uses the current locale and the given +token+ to get a description of that token.
587
+ # @param [Symbol,String] token the token
588
+ # @param [Symbol,String] kind the identifier of a kind
589
+ # @return [String,nil] the descriptive string or +nil+ if something
590
+ # went wrong (e.g. token was not found)
591
+ # @overload token_description(token, kind, locale)
592
+ # Uses the given +locale+ and the given +token+ to get a description of that token.
593
+ # @param [Symbol,String] token the token
594
+ # @param [Symbol,String] kind the identifier of a kind
595
+ # @param [Symbol] locale the locale to use
596
+ # @return [String,nil] the descriptive string or +nil+ if something
597
+ # went wrong (e.g. token was not found)
598
+ def token_description(*args)
599
+ token, kind, locale = tkl_args(args)
600
+ return nil if token.nil? || kind.nil?
601
+
602
+ data_safe(locale).get_description(token, kind)
603
+ end
604
+
605
+ protected
606
+
607
+ # @private
608
+ def data(locale = nil)
609
+ @idb[prep_locale(locale)]
610
+ end
611
+
612
+ # @private
613
+ def data_safe(locale = nil)
614
+ @idb[prep_locale(locale)] || I18n::Inflector::InflectionData_Strict.new(locale)
615
+ end
616
+
617
+ # This method is the internal helper that prepares arguments
618
+ # containing +token+, +kind+ and +locale+.
619
+ #
620
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
621
+ # @raise [ArgumentError] if the count of arguments is invalid
622
+ # @return [Array<Symbol,Symbol,Symbol>] the array containing
623
+ # cleaned and validated +token+, +kind+ and +locale+
624
+ # @overload tkl_args(token, kind, locale)
625
+ # Prepares arguments containing +token+, +kind+ and +locale+.
626
+ # @param [String,Hash] token the token
627
+ # @param [String,Hash] kind the inflection kind
628
+ # @param [String,Hash] locale the locale identifier
629
+ # @return [Array<Symbol,Symbol,Symbol>] the array containing
630
+ # cleaned and validated +token+, +kind+ and +locale+
631
+ # @overload tkl_args(token, kind)
632
+ # Prepares arguments containing +token+ and +locale+.
633
+ # @param [String,Hash] token the token
634
+ # @param [String,Hash] kind the inflection kind
635
+ # @return [Array<Symbol,Symbol,Symbol>] the array containing
636
+ # cleaned and validated +token+, +kind+ and +locale+
637
+ # @overload tkl_args(token)
638
+ # Prepares arguments containing +token+.
639
+ # @param [String,Hash] token the token
640
+ # @return [Array<Symbol,Symbol,Symbol>] the array containing
641
+ # cleaned and validated +token+ and the current locale
642
+ def tkl_args(args)
643
+ token, kind, locale = case args.count
644
+ when 1 then [args[0], nil, nil]
645
+ when 2 then [args[0], args[1], nil]
646
+ when 3 then args
647
+ else raise I18n::ArgumentError, "wrong number of arguments: #{args.count} for (1..3)"
648
+ end
649
+ token = (token.nil? || token.to_s.empty?) ? nil : token.to_sym
650
+ kind = (kind.nil? || kind.to_s.empty?) ? nil : kind.to_sym
651
+ [token, kind, locale]
652
+ end
653
+
654
+ # Processes +locale+ identifier and validates
655
+ # whether it's correct (not empty and not +nil+).
656
+ #
657
+ # @note If the +locale+ is not correct, it
658
+ # tries to use locale from {I18n.locale} and validates it
659
+ # as well.
660
+ # @param [Symbol,String] locale the locale identifier
661
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
662
+ # @return [Symbol] the given locale or the global locale
663
+ def prep_locale(locale = nil)
664
+ locale ||= I18n.locale
665
+ raise I18n::InvalidLocale, locale if locale.to_s.empty?
666
+
667
+ locale.to_sym
668
+ end
669
+ end
670
+ end
671
+ end