hash_with_dot_access 2.0.0 → 2.1.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +4 -1
- data/README.md +3 -0
- data/lib/hash_with_dot_access/version.rb +1 -1
- data/lib/hash_with_dot_access.rb +26 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be6e40c79e1f2c6148e84b6ef6b3521d9c07500bac9603f793a39dac7b3278ce
|
4
|
+
data.tar.gz: ace5ebdd3d951858e6dda87a96cfb9301b4ce7dc9802184cdee1cbd69e664538
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8763ae8a9cd9aec38d1cf2c4e0d659824633295576dfc3132968b82d848f92e354e5fb417f182b5b9be32ba8e2276996e3f36a2e3b8ce71de404c817cd599f1f
|
7
|
+
data.tar.gz: 8ee6d9ba1bdbc2322c40c1a4c79136d43c94376ec167f334123e17844cde1e9364a6a8bee75573827704a12152f328a55bf86980ab249c4170fac9482caa043e
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -55,6 +55,9 @@ hsh.x
|
|
55
55
|
# => 0
|
56
56
|
```
|
57
57
|
|
58
|
+
You can convert a `HashWithDotAccess::Hash` back to a regular Hash with `to_h`, which even works with
|
59
|
+
block enumeration. Or use `to_dot_h` as a `to_h`-like enumerator which preserves dot access.
|
60
|
+
|
58
61
|
## Installation
|
59
62
|
|
60
63
|
Add this line to your application's Gemfile:
|
data/lib/hash_with_dot_access.rb
CHANGED
@@ -13,6 +13,18 @@ module HashWithDotAccess
|
|
13
13
|
value
|
14
14
|
end
|
15
15
|
end
|
16
|
+
|
17
|
+
def self.primitive_value(value)
|
18
|
+
case value
|
19
|
+
when ::Hash
|
20
|
+
value.to_h
|
21
|
+
when Array
|
22
|
+
value = value.dup if value.frozen?
|
23
|
+
value.map! { primitive_value(_1) }
|
24
|
+
else
|
25
|
+
value
|
26
|
+
end
|
27
|
+
end
|
16
28
|
end
|
17
29
|
|
18
30
|
class Hash < ::Hash
|
@@ -138,6 +150,20 @@ module HashWithDotAccess
|
|
138
150
|
def compact
|
139
151
|
dup.tap { _1.compact! }
|
140
152
|
end
|
153
|
+
|
154
|
+
alias_method :_to_h, :to_h
|
155
|
+
|
156
|
+
def to_dot_h(...)
|
157
|
+
self.class.new _to_h(...)
|
158
|
+
end
|
159
|
+
|
160
|
+
def to_h
|
161
|
+
::Hash.new.update(self).to_h do |k, v|
|
162
|
+
value = Utils.primitive_value(v)
|
163
|
+
k, value = yield k, value if block_given?
|
164
|
+
[k, value]
|
165
|
+
end
|
166
|
+
end
|
141
167
|
end
|
142
168
|
|
143
169
|
module Refinements
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_with_dot_access
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bridgetown Team
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-04-
|
11
|
+
date: 2024-04-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|