tater 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tater.rb +30 -7
  3. data/test/tater_test.rb +2 -0
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bce1afa07abcf6f94618ca249808f14d4a16b107c112bc074b4f0e6e2703b86d
4
- data.tar.gz: 3f481fbb8e9551fee057472c3bef0fbc2420c9e267a37e09bf353a19e9877130
3
+ metadata.gz: '00580e7b14eb0c1f1bc26b9a8cc2470638aa06f2f9800b4a4c542de913fffcb4'
4
+ data.tar.gz: f807ce661a2bed8ca9526f309c1548ddf292f034cbcaeae4223a385f89296c86
5
5
  SHA512:
6
- metadata.gz: 9096e096441db2696735213dd21c313aa333877062f3094680a2ff7bbb14c5e3e699d817a8df59626f570eff345b5b69a942ec99cd19ab3bc74cbdee0cb9b398
7
- data.tar.gz: 8511e008cf509bd76869aef9957e98e7f73bb60b50649abe811f09ad4ee01447553689ffe52451f8073110d1338ad430ef7848f25a4e3fce1614c22d60153dc3
6
+ metadata.gz: 8ff891fe31f924f41c5bd20aaa0b58c39659a2e0e72cabd4cfd55df79d2e237daaabe2bb717415eca782f814dcd1322737a861f23fd37449e4f8516a71ea197e
7
+ data.tar.gz: f231dcbca0ca5826a4c99f92afa6c98514985c262a66f473ddb8696d754e849e6c50d371cf82c056e395565b199d6179d665b28b580cade63aa24932c4a378e6
data/lib/tater.rb CHANGED
@@ -91,6 +91,8 @@ class Tater
91
91
  load(messages: messages) if messages
92
92
  end
93
93
 
94
+ # Do lookups cascade by default?
95
+ #
94
96
  # @return [Boolean]
95
97
  def cascades?
96
98
  @cascade
@@ -114,7 +116,7 @@ class Tater
114
116
  # files or a collection of messages.
115
117
  #
116
118
  # @param path [String]
117
- # A path to search for YAML files to load messages from.
119
+ # A path to search for YAML or Ruby files to load messages from.
118
120
  # @param messages [Hash]
119
121
  # A hash of messages ready to be loaded in.
120
122
  def load(path: nil, messages: nil)
@@ -143,6 +145,17 @@ class Tater
143
145
  #
144
146
  # @param object [Date, Time, DateTime, Numeric]
145
147
  # The object to localize.
148
+ # @param options [Hash]
149
+ # Options to configure localization.
150
+ #
151
+ # @option options [String] :format
152
+ # The key or format string to use for localizing the current object.
153
+ # @option options [String] :locale
154
+ # The locale to use in lieu of the current default.
155
+ # @option options [String] :delimiter
156
+ # The delimiter to use when localizing numberic values.
157
+ # @option options [String] :separator
158
+ # The separator to use when localizing numberic values.
146
159
  #
147
160
  # @return [String]
148
161
  # A localized version of the object passed in.
@@ -156,10 +169,10 @@ class Tater
156
169
  when Numeric
157
170
  delimiter = options.delete(:delimiter) || lookup('numeric.delimiter', locale_override)
158
171
  separator = options.delete(:separator) || lookup('numeric.separator', locale_override)
159
- precision = options.fetch(:precision) { 2 }
172
+ precision = options.delete(:precision) || 2
160
173
 
161
- raise(MissingLocalizationFormat, "Numeric localization delimiter ('numeric.delimiter') missing") unless delimiter
162
- raise(MissingLocalizationFormat, "Numeric localization separator ('numeric.separator') missing") unless separator
174
+ raise(MissingLocalizationFormat, "Numeric localization delimiter ('numeric.delimiter') missing or not passed as option :delimiter") unless delimiter
175
+ raise(MissingLocalizationFormat, "Numeric localization separator ('numeric.separator') missing or not passed as option :separator") unless separator
163
176
 
164
177
  # Heavily cribbed from Rails.
165
178
  integer, fraction = Utils.string_from_numeric(object).split('.')
@@ -205,6 +218,8 @@ class Tater
205
218
  # @param key [String]
206
219
  # @param locale_override [String]
207
220
  # A locale to use instead of our current one.
221
+ # @param cascade_override [Boolean]
222
+ # A boolean to forcibly set the cascade option for this lookup.
208
223
  #
209
224
  # @return
210
225
  # Basically anything that can be stored in YAML, including nil.
@@ -234,23 +249,31 @@ class Tater
234
249
  #
235
250
  # @param key [String]
236
251
  # The period-separated key path to look within our messages for.
252
+ # @param options [Hash]
253
+ # Options, including values to interpolate to any found string.
254
+ #
237
255
  # @option options [Boolean] :cascade
256
+ # Should this lookup cascade or not? Can override @cascade.
238
257
  # @option options [String] :default
258
+ # A default string to return, should lookup fail.
239
259
  # @option options [String] :locale
260
+ # A specific locale to lookup within. This will take precedence over the
261
+ # :locales option.
262
+ # @option options [Array<String>] :locales
263
+ # An array of locales to look within.
240
264
  #
241
265
  # @return [String]
242
266
  # The translated and interpreted string, if found, or any data at the
243
267
  # defined key.
244
268
  def translate(key, options = {})
245
269
  cascade_override = options.delete(:cascade)
246
- default = options.delete(:default)
247
270
  locale_override = options.delete(:locale)
248
271
  locales = options.delete(:locales)
249
272
 
250
273
  message =
251
274
  if locale_override || !locales
252
275
  lookup(key, locale_override, cascade_override)
253
- elsif locales
276
+ else
254
277
  locales.find do |accept|
255
278
  found = lookup(key, accept, cascade_override)
256
279
 
@@ -263,7 +286,7 @@ class Tater
263
286
  message = message.call(key, options)
264
287
  end
265
288
 
266
- Utils.interpolate(message, options) || default || "Tater lookup failed: #{ locale_override || locales || locale }.#{ key }"
289
+ Utils.interpolate(message, options) || options.delete(:default) { "Tater lookup failed: #{ locale_override || locales || locale }.#{ key }" }
267
290
  end
268
291
  alias t translate
269
292
  end
data/test/tater_test.rb CHANGED
@@ -116,6 +116,7 @@ describe Tater do
116
116
  it 'cascades' do
117
117
  assert_equal 'Delicious', i18n.lookup('cascade.nope.tacos', nil, true)
118
118
  assert_equal 'Whoaa', i18n.lookup('cascade.another.nope.crazy', nil, true)
119
+ assert_nil i18n.lookup('cascade.another.nope.crazy', nil, false)
119
120
  assert_nil i18n.lookup('cascade.nahhhhhh')
120
121
  end
121
122
  end
@@ -156,6 +157,7 @@ describe Tater do
156
157
  end
157
158
 
158
159
  it 'cascades lookups' do
160
+ assert_equal 'Tater lookup failed: en.cascade.another.nope.crazy', i18n.translate('cascade.another.nope.crazy', cascade: false)
159
161
  assert_equal 'Tater lookup failed: en.cascade.nope.tacos', i18n.translate('cascade.nope.tacos')
160
162
  assert_equal 'Delicious', i18n.translate('cascade.nope.tacos', cascade: true)
161
163
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tater
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Lecklider
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-09 00:00:00.000000000 Z
11
+ date: 2019-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest