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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ad9a4b0a08c33e7b274468ab8220e0540fe64b71761e65972c2a380d5b0c307
4
- data.tar.gz: 94f9dfc578f35de5102e47d49ccb099bb85c72f2270b88c12e8924338d7e7c6a
3
+ metadata.gz: 5902814637c9781346cf05d4d7d091f7107edf9f4d803cae197dfcb6d945996b
4
+ data.tar.gz: 49fbf5bbc2c822222f9a231f69aef347d802b1ddaa02e1b948d8a9fabc9854c2
5
5
  SHA512:
6
- metadata.gz: 745d9aa3d7f0ef67e592789f45de8a2070755ef489ccffc21b1ad723819c4dd28f4727a94a9218326cb2fa454b7947e15c6838f11f4331639b2392afa558ab90
7
- data.tar.gz: 9dd4eee290dae0aff356741c077bdcd3a87d4c7ba654db3abb78933d5c4fdf2250d4d1e43013a0c21b99995b6519fb0e5fe91bace8a85488f582805b61237d79
6
+ metadata.gz: 610a0884b80ca089aa3b796365d57288d4dd8937cf7c3bab65c98e1407887a5f395470e6369f919ebf4eea64c458d805697ee63d677ceab4a62c86f7dd1991e2
7
+ data.tar.gz: db3683f2a0d5fdfd3d389861e4c970589a08df6d33e90fc3ea74c16772adc2b2e64a14bf7d974bb6698e1e33d15817b099705c1e6a9455eee16953e8fdc323dd
data/.gitignore CHANGED
@@ -6,3 +6,6 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ .ruby-version
10
+ .idea/
11
+
data/Gemfile CHANGED
@@ -2,5 +2,5 @@ source "https://rubygems.org"
2
2
 
3
3
  git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
- # Specify your gem's dependencies in deep_struct.gemspec
5
+ # Specify your gem's dependencies in deep_hash_struct.gemspec
6
6
  gemspec
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- deep_hash_struct (0.1.7)
4
+ deep_hash_struct (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 'deep_struct'
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 deep_struct
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.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "deep_struct"
4
+ require "deep_hash_struct"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -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 "deep_struct/version"
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 = DeepStruct::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/deep_struct"
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 "deep_struct/version"
1
+ require "deep_hash_struct/version"
2
2
 
3
3
  # Mixin module for converter.
4
- module DeepStruct
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 { |subkey, subvalue| levels.push([subkey, subset]) if subvalue.is_a?(Hash) }
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
 
@@ -0,0 +1,3 @@
1
+ module DeepHashStruct
2
+ VERSION = "1.0.0"
3
+ 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: 0.1.7
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-19 00:00:00.000000000 Z
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
- - deep_struct.gemspec
75
- - lib/deep_struct.rb
76
- - lib/deep_struct/version.rb
77
- homepage: https://github.com/sayantam/deep_struct
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: {}
@@ -1,3 +0,0 @@
1
- module DeepStruct
2
- VERSION = "0.1.7"
3
- end