recursive_struct 0.0.7 → 0.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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YWI4ZDczZmFhOTYxNWQ2Nzk5MTI3ZGNmMTkzMWQ2M2EwYjU4MzBmYw==
4
+ YTk1NmEwZWJiZWRhZmQ1OTQ1NTI0Y2YxM2ZhOTA3ZTUyZGZkMDc3Nw==
5
5
  data.tar.gz: !binary |-
6
- MzgxZTAyMTQyN2RiNWM4Y2Y4YWZiMTM4OTA4MWRjZDc5ZjRkNTJlMg==
6
+ MzUwZWNjOWMxM2JhY2E4OGUwYzFhZmQ3NmUzYzA4ODdiOTk0YTY2Nw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YjU1M2JhNjE1NGI2ZTM3YzZkNTY3NGVmZjQ0NWFmNmQ0MjJjNjE1MjllNGQ1
10
- M2NjMWExNDM5NTE2NDc1YWQ0ZDVmNWVjYWYzZDg3ODM2MTRlMGJlZjIzMTJl
11
- MWEyYWU5ZWFiYzA1ODgxODkwMTFjNTRiZTQ4OGQxMDk5MDNiY2Y=
9
+ MTMzYTYzZGQ0YzZhMDY2YmU1NTY5YWMyODNjNzg1N2I2NmQ3NTQ5MDk1NWVm
10
+ Nzc5MTE2ZDc2NTA0MmZhNzgzYjk4YmY3NmIyMzQ2NWRkMTQzZmQyMWVkN2Jk
11
+ MDUzZGJlMWMzY2JjMDkzMGVjNzJiMTE3OWVkYzllYjQ4MjA4NzM=
12
12
  data.tar.gz: !binary |-
13
- NDUzYTRiN2FlNDAzYzkwZDc4NDU3OTIxNDQ1NGY4N2ZlNzVjMzZkMjJmNGEw
14
- N2ViNGFkZDJjYTYzMDJmZWVlODdkYzMxMjY5NmJkNjlmZDdkNGMyMDc1ZjIy
15
- YTJiNDc3MzJlOThmM2Q4OGVjMWFlZDhlNTFlMTg0NGUwNzA2MTk=
13
+ NmUyYjJkMWRkY2M2OWJkZmEzNmZkMjNiZTcyNTViZDExZWY5MTc0ODE5OGQ5
14
+ YjMwNzdlNTZmYmEyYjA0ZGJhZmRhMjAzOWNmMzYwNDU4ZmVlNzhhYmRhZTAz
15
+ Y2RhMmJhYzJiMTBiNjliZjlmYzdiN2VhMjRjOTgxMDhjNjQxNDA=
@@ -1,5 +1,9 @@
1
1
  language: ruby
2
- rvm: 2.1.5
2
+ rvm:
3
+ - 1.9
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
3
7
  script: bundle exec rspec spec
4
8
  deploy:
5
9
  provider: rubygems
data/README.md CHANGED
@@ -22,12 +22,14 @@ Or install it yourself as:
22
22
 
23
23
  Passing a hash into the initializer will create an open struct that has all nested getter and setter methods.
24
24
  ```
25
- hash = { a: { b: true }, c: false }
25
+ hash = { a: { b: true }, c: [{ d: true }], e: false }
26
26
  struct = RecursiveStruct.new(hash}
27
- struct.a # #<RecursiveStruct @data={:b=>true}>
28
- struct.a.b # true
29
- struct.c # false
30
- struct.d # nil
27
+ struct.a # #<RecursiveStruct @data={:b=>true}>
28
+ struct.a.b # true
29
+ struct.c # [#<RecursiveStruct @data={:d=>true}>]
30
+ struct.c.first.d # true
31
+ struct.e # false
32
+ struct.f # nil
31
33
  ```
32
34
 
33
35
  You can also use a setter for a method that doesn't exist. A getter and setter will be generated dynamically.
@@ -46,6 +48,15 @@ struct.a = { b: true }
46
48
  struct a # #<RecursiveStruct @data={:b=>true}>
47
49
  ```
48
50
 
51
+ If this value happens to be an array, each hash in the array will be wrapped in a RecursiveStruct.
52
+ ```
53
+ struct = RecursiveStruct.new
54
+ struct.a = [{ b: true }]
55
+ struct.a # [#<RecursiveStruct @data={:b=>true}>]
56
+ struct.a.first.b # true
57
+ struct.a.size # 1
58
+ ```
59
+
49
60
  ## Contributing
50
61
 
51
62
  1. Fork it ( https://github.com/pboksz/recursive_struct/fork )
@@ -22,7 +22,13 @@ class RecursiveStruct
22
22
  private
23
23
 
24
24
  def process(value)
25
- value.is_a?(Hash) ? RecursiveStruct.new(value) : value
25
+ if value.is_a?(Hash)
26
+ RecursiveStruct.new(value)
27
+ elsif value.is_a?(Array)
28
+ value.map { |val| process(val) }
29
+ else
30
+ value
31
+ end
26
32
  end
27
33
 
28
34
  def define_methods(key)
@@ -1,3 +1,3 @@
1
1
  class RecursiveStruct
2
- VERSION = '0.0.7'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -15,12 +15,20 @@ describe RecursiveStruct do
15
15
 
16
16
  describe 'nested hash' do
17
17
  let(:hash) { { one: { two: true } } }
18
+ it { expect(subject.one).to be_a RecursiveStruct }
18
19
  it { expect(subject.one.two).to be true }
19
20
  end
20
21
 
21
22
  describe 'hash with array' do
22
- let(:hash) { { one: { two: [1, 2, 3] } } }
23
- it { expect(subject.one.two).not_to be_empty }
23
+ let(:hash) { { one: [1, 2, 3] } }
24
+ it { expect(subject.one).to be_an Array }
25
+ it { expect(subject.one).not_to be_empty }
26
+ end
27
+
28
+ describe 'hash with array of hashes' do
29
+ let(:hash) { { one: [{ two: true }] } }
30
+ it { expect(subject.one.first).to be_a RecursiveStruct }
31
+ it { expect(subject.one.first.two).to be true }
24
32
  end
25
33
 
26
34
  describe 'proper setter is created' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recursive_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phillip Boksz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-15 00:00:00.000000000 Z
11
+ date: 2015-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler