tater 3.0.2 → 3.0.3

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: b543d8ed6a28b2f4059b2c1fc63593bc2f7ced7b2cc9d62e786c78263b97ec35
4
- data.tar.gz: 0042276af4078a436e7f34e1f50500e5dd92649ae060b24921a98f5dfc859789
3
+ metadata.gz: 7176a93dd72c2135f8114c2baed2d4fdda8ffcb7c02312517810d94f9e275dce
4
+ data.tar.gz: 5dea4c80c2a894063790d2cd4ed2232e8566b3cae4a3ab9a73a2be3c45ffd4f5
5
5
  SHA512:
6
- metadata.gz: cd927d5425f5a8b7356fafb72db449d57494042369eb9ba0018a2ba7d68893fe24c0d633d75a93ab8ff1d3e8f2367d148e87c5bc068ec1717afa27d1168cec8c
7
- data.tar.gz: 4f0a917429aef2c443288f9c338a0471f86fb608585e29e5fdc8b3f04b8ecfe654ecdc7e09daed29e99b19ddb3bf1a4175057d6db77c64b495b25a232f2c6eb7
6
+ metadata.gz: b32716e497cc9a2e81eded3bc277450d143ffec829b4accf3b3396b2c1afb28b4c66bab044695cf13bd6b64ff830d4168b181435565864e977f59ce9fcf6df1e
7
+ data.tar.gz: bd697439d8db3cd1607762bd92adb13e066fb40e16c7db6469a8e2a226774480a4cd1ba19651d45673c6ce819e9f7913af82c0b779c37c5f803e0c72ce3d42ff
@@ -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,8 +3,9 @@ require 'bigdecimal'
3
3
  require 'date'
4
4
  require 'time'
5
5
  require 'yaml'
6
- require_relative 'tater/utils'
7
- require_relative 'tater/hash' unless Hash.method_defined?(:except)
6
+
7
+ require 'tater/utils'
8
+ require 'tater/hash' unless Hash.method_defined?(:except)
8
9
 
9
10
  # Tater is a internationalization (i18n) and localization (l10n) library
10
11
  # designed for speed and simplicity.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tater
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Lecklider
@@ -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