tater 2.0.3 → 2.0.4

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tater.rb +27 -44
  3. data/test/tater_test.rb +4 -2
  4. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e5ec34021f3488e2ef8ef3365abc37258813e5d9a79b8dbd0a49fb8254c946a
4
- data.tar.gz: 8613b2a54d59d0b4a64f84790e60f55316447714d6013a6751041a2aa9dada6a
3
+ metadata.gz: 51644f41d80e87d20ed43b9e8c6b4797fb7aeb4258c94d5d9f9d7a50eb5ee5c6
4
+ data.tar.gz: 1fe43b99fbc2f24cdc342ec4278ff7140ee6b9579dd51d236fcc713907f1455d
5
5
  SHA512:
6
- metadata.gz: f65062d798ace02b68084b1a9189f09c092e32f2a819bd88909d651038a205a36b60816f8c6e890614c9aaea1a17e074b89a7f3a5163969bf10c6f35a8eac56e
7
- data.tar.gz: cf3ae7da9a2d911f90aa93dd1f2e088425becf21d2b5558c485d86002bcccf09a4bdccfed39385442a7865a27e76196aa68bf90b259ea30f5b8beeda8eb1807c
6
+ metadata.gz: d4c059fdc696d39c489e61d3e53954fe921feaba78055bd5e62c944d45daf10dfa178b76e4e836d326ba61be0dbf7bbec13b63f52b95e13f23da9ca5be4d3b8c
7
+ data.tar.gz: 6b12ffa8f2a1186f2de92c9c16bb5c2e1027f93ab5ee2c877dbad297777274b9f4f38882255490db3c555a0dafb129ed876bd784d3a872e07a4e7e9f6e0e24ec
data/lib/tater.rb CHANGED
@@ -109,7 +109,6 @@ class Tater
109
109
  # @param path [String]
110
110
  # A path to search for YAML or Ruby files to load messages from.
111
111
  def initialize(cascade: false, locale: nil, messages: nil, path: nil)
112
- @cache = {}
113
112
  @cascade = cascade
114
113
  @locale = locale
115
114
  @messages = {}
@@ -167,8 +166,10 @@ class Tater
167
166
 
168
167
  # Not only does this clear our cache but it establishes the basic structure
169
168
  # that we rely on in other methods.
169
+ @cache = {}
170
+
170
171
  @messages.each_key do |key|
171
- @cache[key] = { true => {}, false => {} }
172
+ @cache[key] = { false => {}, true => {} }
172
173
  end
173
174
  end
174
175
 
@@ -223,37 +224,45 @@ class Tater
223
224
 
224
225
  # Lookup a key in the messages hash, using the current locale or an override.
225
226
  #
227
+ # @example Using the default locale, look up a key's value.
228
+ # i18n = Tater.new(locale: 'en', messages: { 'en' => { 'greeting' => { 'world' => 'Hello, world!' } } })
229
+ # i18n.lookup('greeting.world') # => "Hello, world!"
230
+ #
226
231
  # @param key [String]
232
+ # The period-separated key path to look for within our messages.
227
233
  # @param locale [String]
228
- # A locale to use instead of our current one.
234
+ # A locale to use instead of our current one, if any.
229
235
  # @param cascade [Boolean]
230
236
  # A boolean to forcibly set the cascade option for this lookup.
231
237
  #
232
238
  # @return
233
239
  # Basically anything that can be stored in your messages Hash.
234
240
  def lookup(key, locale: nil, cascade: nil)
235
- locale = locale.nil? ? @locale : locale
236
- cascade = cascade.nil? ? @cascade : cascade
237
-
238
- cached(key, locale, cascade) || begin
239
- return nil unless @messages.key?(locale.to_s)
241
+ locale =
242
+ if locale.nil?
243
+ @locale
244
+ else
245
+ locale.to_s
246
+ end
240
247
 
241
- path = key.split(SEPARATOR).prepend(locale).map(&:to_s)
248
+ cascade = @cascade if cascade.nil?
242
249
 
243
- message =
244
- if cascade
245
- while path.length >= 2
246
- attempt = @messages.dig(*path)
250
+ @cache[locale][cascade][key] ||= begin
251
+ path = key.split(SEPARATOR)
247
252
 
248
- break attempt unless attempt.nil?
253
+ message = @messages[locale].dig(*path)
249
254
 
255
+ if message.nil? && cascade
256
+ message =
257
+ while path.length > 1
250
258
  path.delete_at(path.length - 2)
259
+ attempt = @messages[locale].dig(*path)
260
+
261
+ break attempt unless attempt.nil?
251
262
  end
252
- else
253
- @messages.dig(*path)
254
- end
263
+ end
255
264
 
256
- cache(key, locale, cascade, message)
265
+ message
257
266
  end
258
267
  end
259
268
 
@@ -338,32 +347,6 @@ class Tater
338
347
 
339
348
  private
340
349
 
341
- # @param key [String]
342
- # The cache key, often in the form "something.nested.like.this"
343
- # @param locale [String]
344
- # The locale to store the value for.
345
- # @param cascade [Boolean]
346
- # Was this a cascading lookup?
347
- # @param message [String]
348
- # The message being cached, often a String.
349
- # @return [String]
350
- # Whatever value is being cached, often a String.
351
- def cache(key, locale, cascade, message)
352
- @cache[locale][cascade][key] = message
353
- end
354
-
355
- # @param key [String]
356
- # The cache key, often in the form "something.nested.like.this"
357
- # @param locale [String]
358
- # The locale to store the value for.
359
- # @param cascade [Boolean]
360
- # Was this a cascading lookup?
361
- # @return [String, nil]
362
- # The cached message or nil.
363
- def cached(key, locale, cascade)
364
- @cache.dig(locale, cascade, key)
365
- end
366
-
367
350
  # Localize an Array object.
368
351
  #
369
352
  # @param object [Array<String>]
data/test/tater_test.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
- require_relative '../lib/tater'
3
- require 'minitest/autorun'
2
+ $LOAD_PATH.unshift File.expand_path('lib', __dir__)
3
+
4
4
  require 'date'
5
+ require 'minitest/autorun'
6
+ require 'tater'
5
7
 
6
8
  describe Tater do
7
9
  describe Tater::Utils do
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: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Lecklider
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-22 00:00:00.000000000 Z
11
+ date: 2021-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-packaging
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rubocop-performance
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -144,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
158
  - !ruby/object:Gem::Version
145
159
  version: '2.0'
146
160
  requirements: []
147
- rubygems_version: 3.2.15
161
+ rubygems_version: 3.2.22
148
162
  signing_key:
149
163
  specification_version: 4
150
164
  summary: Minimal internationalization and localization library.