deep_hash_struct 0.1.7 → 1.0.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/.gitignore +3 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +18 -3
- data/bin/console +1 -1
- data/{deep_struct.gemspec → deep_hash_struct.gemspec} +3 -3
- data/lib/{deep_struct.rb → deep_hash_struct.rb} +8 -3
- data/lib/deep_hash_struct/version.rb +3 -0
- metadata +6 -6
- data/lib/deep_struct/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5902814637c9781346cf05d4d7d091f7107edf9f4d803cae197dfcb6d945996b
|
4
|
+
data.tar.gz: 49fbf5bbc2c822222f9a231f69aef347d802b1ddaa02e1b948d8a9fabc9854c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 610a0884b80ca089aa3b796365d57288d4dd8937cf7c3bab65c98e1407887a5f395470e6369f919ebf4eea64c458d805697ee63d677ceab4a62c86f7dd1991e2
|
7
|
+
data.tar.gz: db3683f2a0d5fdfd3d389861e4c970589a08df6d33e90fc3ea74c16772adc2b2e64a14bf7d974bb6698e1e33d15817b099705c1e6a9455eee16953e8fdc323dd
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -8,7 +8,7 @@ object will have same level of nesting.
|
|
8
8
|
Add this line to your application's Gemfile:
|
9
9
|
|
10
10
|
```ruby
|
11
|
-
gem '
|
11
|
+
gem 'deep_hash_struct'
|
12
12
|
```
|
13
13
|
|
14
14
|
And then execute:
|
@@ -17,17 +17,32 @@ And then execute:
|
|
17
17
|
|
18
18
|
Or install it yourself as:
|
19
19
|
|
20
|
-
$ gem install
|
20
|
+
$ gem install deep_hash_struct
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
24
|
+
### Example 1
|
24
25
|
```ruby
|
25
|
-
input = { x: 1, y: { z: 2} }
|
26
|
+
input = { x: 1, y: { z: 2 } }
|
26
27
|
point = deep_struct(input)
|
27
28
|
point.x #=> 1
|
28
29
|
point.y.z #=> 2
|
29
30
|
```
|
30
31
|
|
32
|
+
### Example 2
|
33
|
+
```ruby
|
34
|
+
input = { foo: [{ x: [{ y: { z: 1 } }] }] }
|
35
|
+
obj = deep_struct(input)
|
36
|
+
obj.foo[0].x[0].y.z #=> 1
|
37
|
+
```
|
38
|
+
|
39
|
+
### Example 3
|
40
|
+
```ruby
|
41
|
+
input = { foo: { bar: [{ x: 1 }]} }
|
42
|
+
obj = deep_struct(input)
|
43
|
+
obj.foo.bar[0].x #=> 1
|
44
|
+
```
|
45
|
+
|
31
46
|
## Development
|
32
47
|
|
33
48
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/bin/console
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
|
2
2
|
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "
|
4
|
+
require "deep_hash_struct/version"
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "deep_hash_struct"
|
8
|
-
spec.version =
|
8
|
+
spec.version = DeepHashStruct::VERSION
|
9
9
|
spec.authors = ["Sayantam Dey"]
|
10
10
|
spec.email = ["sayantam@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{Converts a (possibly nested) Hash to a (nested) Struct.}
|
13
|
-
spec.homepage = "https://github.com/sayantam/
|
13
|
+
spec.homepage = "https://github.com/sayantam/deep_hash_struct"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
# Specify which files should be added to the gem when it is released.
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require "
|
1
|
+
require "deep_hash_struct/version"
|
2
2
|
|
3
3
|
# Mixin module for converter.
|
4
|
-
module
|
4
|
+
module DeepHashStruct
|
5
5
|
# Converts a Hash to a Struct. If the Hash has nested Hash objects, they are converted
|
6
6
|
# to Struct as well.
|
7
7
|
# output = deep_struct({x: 1, y: {z: 2}})
|
@@ -18,7 +18,12 @@ module DeepStruct
|
|
18
18
|
# overwrite the shared reference with input by a new reference
|
19
19
|
context[key] = context[key].clone
|
20
20
|
subset = context[key]
|
21
|
-
subset.each
|
21
|
+
subset.each do |subkey, subvalue|
|
22
|
+
levels.push([subkey, subset]) if subvalue.is_a?(Hash)
|
23
|
+
subset[subkey] = subset[subkey].map { |v| v.is_a?(Hash) ? deep_struct(v) : v } if subvalue.is_a?(Array)
|
24
|
+
end
|
25
|
+
elsif context[key].is_a?(Array)
|
26
|
+
context[key] = context[key].map { |v| v.is_a?(Hash) ? deep_struct(v) : v }
|
22
27
|
end
|
23
28
|
end
|
24
29
|
|
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: 0.
|
4
|
+
version: 1.0.0
|
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-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,10 +71,10 @@ files:
|
|
71
71
|
- Rakefile
|
72
72
|
- bin/console
|
73
73
|
- bin/setup
|
74
|
-
-
|
75
|
-
- lib/
|
76
|
-
- lib/
|
77
|
-
homepage: https://github.com/sayantam/
|
74
|
+
- deep_hash_struct.gemspec
|
75
|
+
- lib/deep_hash_struct.rb
|
76
|
+
- lib/deep_hash_struct/version.rb
|
77
|
+
homepage: https://github.com/sayantam/deep_hash_struct
|
78
78
|
licenses:
|
79
79
|
- MIT
|
80
80
|
metadata: {}
|
data/lib/deep_struct/version.rb
DELETED