hash_to_struct 0.1.0 → 0.1.1
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 +6 -0
- data/lib/hash_to_struct/struct.rb +13 -4
- data/lib/hash_to_struct/version.rb +1 -1
- 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: f85aaeb54564d1fb5350a2545cd5117b74ec826c8119cb81981875a12e96f8e8
|
4
|
+
data.tar.gz: b58404ae47204c1d6ba18da843c6526b2fd90d678e8aa668fd125febfeec7168
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c84576fc423c375963361beb0ae9c2b704a926084192f76ec27ec4b95884119e45edefdf056fafbd0a120766420043feee91d2361dca5c0b1abf990119d7c200
|
7
|
+
data.tar.gz: ca81da797059808926fbe6b44d985c500c8b7df7bcf79e6dd98aa102d18750120f0e85c6c3dd7d26cd49a811dce7ccad3cf7ce3531a870e1db02c67011cc5d92
|
data/CHANGELOG.md
CHANGED
@@ -25,11 +25,11 @@ module HashToStruct
|
|
25
25
|
|
26
26
|
def to_struct(hash)
|
27
27
|
h = hash.each_key.each_with_object({}) do |key, h|
|
28
|
-
create_new = -> (
|
28
|
+
create_new = -> (v) { is_hash?(v) ? self.class.new(v, **@options) : v}
|
29
29
|
h[key] = case
|
30
|
-
when hash[key]
|
30
|
+
when is_hash?(hash[key])
|
31
31
|
create_new.call(hash[key])
|
32
|
-
when @options[:including_arrays] && hash[key]
|
32
|
+
when @options[:including_arrays] && is_array?(hash[key])
|
33
33
|
hash[key].map(&create_new)
|
34
34
|
else
|
35
35
|
hash[key]
|
@@ -43,12 +43,21 @@ module HashToStruct
|
|
43
43
|
h.keys.each do |key|
|
44
44
|
h[key] = case
|
45
45
|
when h[key].is_a?(self.class) then h[key].to_h
|
46
|
-
when h[key]
|
46
|
+
when is_array?(h[key])
|
47
|
+
h[key].map { |v| v.is_a?(self.class) ? v.to_h : v }
|
47
48
|
else
|
48
49
|
h[key]
|
49
50
|
end
|
50
51
|
end
|
51
52
|
end
|
52
53
|
end
|
54
|
+
|
55
|
+
def is_hash?(val)
|
56
|
+
val.class == Hash
|
57
|
+
end
|
58
|
+
|
59
|
+
def is_array?(val)
|
60
|
+
val.class == Array
|
61
|
+
end
|
53
62
|
end
|
54
63
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_to_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Bohush
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-01-
|
11
|
+
date: 2022-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|