deep_hash_struct 1.0.0 → 1.0.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/.github/workflows/gem-push.yml +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +5 -1
- data/lib/deep_hash_struct.rb +20 -5
- data/lib/deep_hash_struct/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a204988b6492187bef2399e43151ceba1256b0871960da4e5c2da150d866cac1
|
4
|
+
data.tar.gz: d903d60b5c8e9c3fc7033b76e3a80021e00406d9ca1f2cc199e334371d8c3e97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1d6487e51e143dfc47646cf333578e98516216e2cee7f2da3b81016ad7ae1e5670d391c14e4c9a9e324a22036d6fc41c3e8f63020afec52b8ae849a19724213
|
7
|
+
data.tar.gz: 1b041df58317be02eb640a9e05373f1f038c9c2cbbc76d2f354ef2420edc5296dc10e50010077dcd1d2f5007caf9835c828cd021ede7b059c0d7f50db1a1642d
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# DeepHashStruct
|
2
2
|
|
3
3
|
Utility function to convert a Hash to a Struct. The Hash can be arbitrarily nested, the corresponding
|
4
4
|
object will have same level of nesting.
|
@@ -21,6 +21,10 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
24
|
+
```ruby
|
25
|
+
include DeepHashStruct
|
26
|
+
```
|
27
|
+
|
24
28
|
### Example 1
|
25
29
|
```ruby
|
26
30
|
input = { x: 1, y: { z: 2 } }
|
data/lib/deep_hash_struct.rb
CHANGED
@@ -12,6 +12,13 @@ module DeepHashStruct
|
|
12
12
|
# @return [Struct]
|
13
13
|
def deep_struct(input)
|
14
14
|
output = input.clone
|
15
|
+
levels = expand_levels(output)
|
16
|
+
collapse_levels(levels)
|
17
|
+
klass = Struct.new(*output.keys, keyword_init: true)
|
18
|
+
klass.new(output)
|
19
|
+
end
|
20
|
+
|
21
|
+
def expand_levels(output)
|
15
22
|
levels = output.keys.map { |key| [key, output] }
|
16
23
|
levels.each do |key, context|
|
17
24
|
if context[key].is_a?(Hash)
|
@@ -20,21 +27,29 @@ module DeepHashStruct
|
|
20
27
|
subset = context[key]
|
21
28
|
subset.each do |subkey, subvalue|
|
22
29
|
levels.push([subkey, subset]) if subvalue.is_a?(Hash)
|
23
|
-
subset[subkey] = subset[subkey]
|
30
|
+
subset[subkey] = deep_struct_enumerable(subset[subkey]) if subvalue.is_a?(Array)
|
24
31
|
end
|
25
32
|
elsif context[key].is_a?(Array)
|
26
|
-
context[key] = context[key]
|
33
|
+
context[key] = deep_struct_enumerable(context[key])
|
27
34
|
end
|
28
35
|
end
|
36
|
+
levels
|
37
|
+
end
|
38
|
+
private :expand_levels
|
29
39
|
|
40
|
+
# @param [Enumerable] enumerable
|
41
|
+
def deep_struct_enumerable(enumerable)
|
42
|
+
enumerable.map { |v| v.is_a?(Hash) ? deep_struct(v) : v }
|
43
|
+
end
|
44
|
+
private :deep_struct_enumerable
|
45
|
+
|
46
|
+
def collapse_levels(levels)
|
30
47
|
levels.reverse_each do |k, context|
|
31
48
|
if context[k].is_a?(Hash)
|
32
49
|
klass = Struct.new(*context[k].keys, keyword_init: true)
|
33
50
|
context[k] = klass.new(context[k])
|
34
51
|
end
|
35
52
|
end
|
36
|
-
|
37
|
-
klass = Struct.new(*output.keys, keyword_init: true)
|
38
|
-
klass.new(output)
|
39
53
|
end
|
54
|
+
private :collapse_levels
|
40
55
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deep_hash_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sayantam Dey
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -93,7 +93,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
- !ruby/object:Gem::Version
|
94
94
|
version: '0'
|
95
95
|
requirements: []
|
96
|
-
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.7.6.2
|
97
98
|
signing_key:
|
98
99
|
specification_version: 4
|
99
100
|
summary: Converts a (possibly nested) Hash to a (nested) Struct.
|