recursive_struct 0.0.7 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.travis.yml +5 -1
- data/README.md +16 -5
- data/lib/recursive_struct/data.rb +7 -1
- data/lib/recursive_struct/version.rb +1 -1
- data/spec/recursive_struct_spec.rb +10 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YTk1NmEwZWJiZWRhZmQ1OTQ1NTI0Y2YxM2ZhOTA3ZTUyZGZkMDc3Nw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MzUwZWNjOWMxM2JhY2E4OGUwYzFhZmQ3NmUzYzA4ODdiOTk0YTY2Nw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MTMzYTYzZGQ0YzZhMDY2YmU1NTY5YWMyODNjNzg1N2I2NmQ3NTQ5MDk1NWVm
|
10
|
+
Nzc5MTE2ZDc2NTA0MmZhNzgzYjk4YmY3NmIyMzQ2NWRkMTQzZmQyMWVkN2Jk
|
11
|
+
MDUzZGJlMWMzY2JjMDkzMGVjNzJiMTE3OWVkYzllYjQ4MjA4NzM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NmUyYjJkMWRkY2M2OWJkZmEzNmZkMjNiZTcyNTViZDExZWY5MTc0ODE5OGQ5
|
14
|
+
YjMwNzdlNTZmYmEyYjA0ZGJhZmRhMjAzOWNmMzYwNDU4ZmVlNzhhYmRhZTAz
|
15
|
+
Y2RhMmJhYzJiMTBiNjliZjlmYzdiN2VhMjRjOTgxMDhjNjQxNDA=
|
data/.travis.yml
CHANGED
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
|
28
|
-
struct.a.b
|
29
|
-
struct.c
|
30
|
-
struct.d
|
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)
|
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)
|
@@ -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:
|
23
|
-
it { expect(subject.one
|
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
|
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:
|
11
|
+
date: 2015-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|