tater 3.0.0 → 3.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c34d7058c3566313243c4ccb7b99d967c60bf2e00e091d1fbb66dfbd257e853f
4
- data.tar.gz: 0a5c1d4df6a34b9953086af0459af4c72cb8d9faf674d3216e1eb9e00aa0e82f
3
+ metadata.gz: 1913c7bc9ecb58cbb7ff7c4239978c8401116a954f967e1fd1e557ad30b0e7bd
4
+ data.tar.gz: cd65a35921e999375affaf4a38717fc14c6938285f3ad1d4258727494a212d40
5
5
  SHA512:
6
- metadata.gz: ac657b28f3e3421def12e3d0b50bcd895c5d1a1b23de7c97da6bf0e20bc5e0a51e20abf7394ec9e99b2137e9b2261392213f0dc85d2d3b0c598c4e73184d8857
7
- data.tar.gz: 4b5b32cf7b23a66afcde0a73f129a76d791f62a8efa45a7223204d9e017328796cf528ad8884a2c25a568ea47d3047fff57c4973741aaae20e1e7574350a9068
6
+ metadata.gz: 8744cffa6df1821728ee83f221899d7b5dae739c9420f0ce40bbd02f9b7a7d1388aeed8f54a3d029663a2843b0fca915cd277dc688b044a559c994630695bfc1
7
+ data.tar.gz: 4e0cfde2f00feaf6e3ce76aec920b0f56a208dfc59e18f0af0c679fe1f8d733ce6c5343bdafc36df33bb2170b6ac660e36d906161273ddf7a3c9fc0a96cf78aa
data/README.org CHANGED
@@ -212,8 +212,8 @@ i18n.translate('login.special.description', cascade: true) # => 'Normal descript
212
212
  With cascade, the final key stays the same, but pieces of the scope get lopped
213
213
  off. In this case, lookups will be tried in this order:
214
214
 
215
- 1. ='login.special.description'=
216
- 2. ='login.description'=
215
+ 1. =login.special.description=
216
+ 2. =login.description=
217
217
 
218
218
  This can be useful when you want to override some messages but don't want to
219
219
  have to copy all of the other, non-overwritten messages.
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ require 'tater'
3
+
4
+ class Tater # :nodoc:
5
+ # Alias to Tater#localize
6
+ alias l localize
7
+
8
+ # Alias to Tater#translate
9
+ alias t translate
10
+ end
data/lib/tater/hash.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+ class Tater
3
+ # Refine Hash and add the #except method from Ruby 3.
4
+ module HashExcept
5
+ refine Hash do
6
+ # Taken from the excellent backports gem written by Marc-André Lafortune.
7
+ # https://github.com/marcandre/backports/blob/master/lib/backports/3.0.0/hash/except.rb
8
+ def except(*keys)
9
+ if keys.size > 4 && size > 4 # index if O(m*n) is big
10
+ h = {}
11
+ keys.each { |key| h[key] = true }
12
+ keys = h
13
+ end
14
+
15
+ reject { |key, _value| keys.include?(key) }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+ class Tater
3
+ # Utility methods that require no state.
4
+ module Utils
5
+ HASH = {}.freeze
6
+ FORMAT_CURLY = '%{'
7
+ FORMAT_NAMED = '%<'
8
+
9
+ # Merge all the way down.
10
+ #
11
+ # @param to [Hash]
12
+ # The target Hash to merge into.
13
+ # @param from [Hash]
14
+ # The Hash to copy values from.
15
+ # @return [Hash]
16
+ def self.deep_merge(to, from)
17
+ to.merge(from) do |_key, left, right|
18
+ if left.is_a?(Hash) && right.is_a?(Hash)
19
+ Utils.deep_merge(left, right)
20
+ else
21
+ right
22
+ end
23
+ end
24
+ end
25
+
26
+ # Transform keys all the way down.
27
+ #
28
+ # @param hash [Hash]
29
+ # The Hash to stringify keys for.
30
+ # @return [Hash]
31
+ def self.deep_stringify_keys(hash)
32
+ hash.transform_keys(&:to_s).transform_values do |value|
33
+ if value.is_a?(Hash)
34
+ Utils.deep_stringify_keys(value)
35
+ else
36
+ value
37
+ end
38
+ end
39
+ end
40
+
41
+ # Freeze all the way down.
42
+ #
43
+ # @param hash [Hash]
44
+ # @return [Hash]
45
+ def self.deep_freeze(hash)
46
+ hash.transform_keys(&:freeze).transform_values do |value|
47
+ if value.is_a?(Hash)
48
+ Utils.deep_freeze(value)
49
+ else
50
+ value.freeze
51
+ end
52
+ end.freeze
53
+ end
54
+
55
+ # Format values into a string if appropriate.
56
+ #
57
+ # @param string [String]
58
+ # The target string to interpolate into.
59
+ # @param options [Hash]
60
+ # The values to interpolate into the target string.
61
+ #
62
+ # @return [String]
63
+ def self.interpolate(string, options = HASH)
64
+ return string if options.empty?
65
+ return string unless interpolation_string?(string)
66
+
67
+ format(string, options)
68
+ end
69
+
70
+ # Determine whether a string includes any interpolation placeholders e.g.
71
+ # "%{" or "%<"
72
+ #
73
+ # @param string [String]
74
+ # @return [Boolean]
75
+ def self.interpolation_string?(string)
76
+ string.include?(FORMAT_CURLY) || string.include?(FORMAT_NAMED)
77
+ end
78
+
79
+ # Convert a Numeric to a string, particularly formatting BigDecimals to a
80
+ # Float-like string representation.
81
+ #
82
+ # @param numeric [Numeric]
83
+ #
84
+ # @return [String]
85
+ def self.string_from_numeric(numeric)
86
+ if numeric.is_a?(BigDecimal)
87
+ numeric.to_s('F')
88
+ else
89
+ numeric.to_s
90
+ end
91
+ end
92
+ end
93
+ end
data/lib/tater.rb CHANGED
@@ -3,7 +3,9 @@ require 'bigdecimal'
3
3
  require 'date'
4
4
  require 'time'
5
5
  require 'yaml'
6
+
6
7
  require 'tater/utils'
8
+ require 'tater/hash' unless Hash.method_defined?(:except)
7
9
 
8
10
  # Tater is a internationalization (i18n) and localization (l10n) library
9
11
  # designed for speed and simplicity.
@@ -17,6 +19,9 @@ class Tater
17
19
  SEPARATOR = '.'
18
20
  SUBSTITUTION_REGEX = /%(|\^)[aAbBpP]/.freeze
19
21
 
22
+ # Needed for Ruby < 3.
23
+ using HashExcept unless Hash.method_defined?(:except)
24
+
20
25
  # @return [String]
21
26
  attr_reader :locale
22
27
 
@@ -205,20 +210,24 @@ class Tater
205
210
  #
206
211
  # @return [Boolean]
207
212
  def includes?(key, options = HASH)
208
- message =
209
- if options.key?(:locales)
210
- options[:locales].append(@locale) if @locale && !options[:locales].include?(@locale)
213
+ if options.empty?
214
+ !lookup(key).nil?
215
+ else
216
+ message =
217
+ if options.key?(:locales)
218
+ options[:locales].append(@locale) if @locale && !options[:locales].include?(@locale)
211
219
 
212
- options[:locales].find do |accept|
213
- found = lookup(key, locale: accept, cascade: options[:cascade])
220
+ options[:locales].find do |accept|
221
+ found = lookup(key, locale: accept, cascade: options[:cascade])
214
222
 
215
- break found unless found.nil?
223
+ break found unless found.nil?
224
+ end
225
+ else
226
+ lookup(key, locale: options[:locale], cascade: options[:cascade])
216
227
  end
217
- else
218
- lookup(key, locale: options[:locale], cascade: options[:cascade])
219
- end
220
228
 
221
- !message.nil?
229
+ !message.nil?
230
+ end
222
231
  end
223
232
 
224
233
  # Translate a key path and optional interpolation arguments into a string.
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: 3.0.0
4
+ version: 3.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-12-18 00:00:00.000000000 Z
11
+ date: 2021-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -132,6 +132,9 @@ files:
132
132
  - LICENSE.txt
133
133
  - README.org
134
134
  - lib/tater.rb
135
+ - lib/tater/aliases.rb
136
+ - lib/tater/hash.rb
137
+ - lib/tater/utils.rb
135
138
  - test/fixtures/another.yml
136
139
  - test/fixtures/fixtures.yml
137
140
  - test/fixtures/messages/more.yml