recursive-open-struct 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/Gemfile +2 -0
- data/README.md +9 -9
- data/lib/recursive_open_struct.rb +15 -9
- data/lib/recursive_open_struct/version.rb +1 -1
- data/spec/recursive_open_struct_spec.rb +48 -3
- data/spec/spec_helper.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad6b86b4a8ed2c69543135a33097d06f13e92671
|
4
|
+
data.tar.gz: bb738b78cb40ff95bb7d257cda73a68b5dec6f7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd1efd0c19bc1e6418592d071b32fcf5df03a341b852f6e1c4a5a75f7cf120494135a518011348848718c960e96414e1db033a985e388dca1060790fd7f9e922
|
7
|
+
data.tar.gz: 8a2e0a8592dac362652a0e8adb369b4b611df7cdbab37c55dd8cae75bad1ca6cc1976f11fedfa4d751fa0ae099e0a44551368ae838e76cedb3591ab06585aae1
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
0.6.1 / 2015-03-28
|
2
|
+
==================
|
3
|
+
|
4
|
+
* FIX: Actually ensure that the internal @table is properly dependent or
|
5
|
+
independent of the input hash tree. I mistakenly refactored away an important
|
6
|
+
piece of code that fervic added.
|
7
|
+
* FIX: Actually ensure that `#dup` works.
|
8
|
+
* Also refactored how `#to_h` is implemented to use newer plumbing.
|
9
|
+
|
1
10
|
0.6.0 / 2015-03-28
|
2
11
|
==================
|
3
12
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -5,23 +5,23 @@ RecursiveOpenStructs.
|
|
5
5
|
|
6
6
|
It allows for hashes within hashes to be called in a chain of methods:
|
7
7
|
|
8
|
-
|
8
|
+
ros = RecursiveOpenStruct.new( { :fooa => { :foob => 'fooc' } } )
|
9
9
|
|
10
|
-
|
10
|
+
ros.fooa.foob # => 'fooc'
|
11
11
|
|
12
12
|
Also, if needed, nested hashes can still be accessed as hashes:
|
13
13
|
|
14
|
-
|
14
|
+
ros.fooa_as_a_hash # { :foob => 'fooc' }
|
15
15
|
|
16
16
|
RecursiveOpenStruct can also optionally recurse across arrays, although you
|
17
17
|
have to explicitly enable it:
|
18
18
|
|
19
|
-
|
19
|
+
h = { :somearr => [ { :name => 'a'}, { :name => 'b' } ] }
|
20
20
|
|
21
|
-
|
21
|
+
ros = RecursiveOpenStruct.new(h, :recurse_over_arrays => true )
|
22
22
|
|
23
|
-
|
24
|
-
|
23
|
+
ros.somarr[0].name # => 'a'
|
24
|
+
ros.somarr[1].name # => 'b'
|
25
25
|
|
26
26
|
## Installation
|
27
27
|
|
@@ -29,11 +29,11 @@ Available as a gem in rubygems, the default gem repository.
|
|
29
29
|
|
30
30
|
If you use bundler, just throw that in your gemfile :
|
31
31
|
|
32
|
-
|
32
|
+
gem 'recursive-open-struct'
|
33
33
|
|
34
34
|
You may also install the gem manually :
|
35
35
|
|
36
|
-
|
36
|
+
gem install recursive-open-struct
|
37
37
|
|
38
38
|
## Contributing
|
39
39
|
|
@@ -17,19 +17,25 @@ class RecursiveOpenStruct < OpenStruct
|
|
17
17
|
|
18
18
|
super(hash)
|
19
19
|
|
20
|
+
if mutate_input_hash && hash
|
21
|
+
hash.clear
|
22
|
+
@table.each { |k,v| hash[k] = v }
|
23
|
+
@table = hash
|
24
|
+
end
|
25
|
+
|
26
|
+
@sub_elements = {}
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize_copy(orig)
|
30
|
+
super
|
31
|
+
# deep copy the table to separate the two objects
|
32
|
+
@table = DeepDup.new(recurse_over_arrays: @recurse_over_arrays).call(orig.instance_variable_get(:@table))
|
33
|
+
# Forget any memoized sub-elements
|
20
34
|
@sub_elements = {}
|
21
35
|
end
|
22
36
|
|
23
37
|
def to_h
|
24
|
-
@
|
25
|
-
if newval.kind_of?(self.class)
|
26
|
-
newval.to_h
|
27
|
-
elsif newval.kind_of?(Array)
|
28
|
-
newval.map { |a| a.kind_of?(self.class) ? a.to_h : a }
|
29
|
-
else
|
30
|
-
raise "Cached value of unsupported type: #{newval.inspect}"
|
31
|
-
end
|
32
|
-
end
|
38
|
+
DeepDup.new(recurse_over_arrays: @recurse_over_arrays).call(@table)
|
33
39
|
end
|
34
40
|
|
35
41
|
alias_method :to_hash, :to_h
|
@@ -110,11 +110,19 @@ describe RecursiveOpenStruct do
|
|
110
110
|
|
111
111
|
before(:each) { subject.blah.blargh = "Janet" }
|
112
112
|
|
113
|
-
|
114
|
-
|
113
|
+
describe ".to_h" do
|
114
|
+
it "returns a hash tree that contains those modifications" do
|
115
|
+
subject.to_h.should == updated_hash
|
116
|
+
end
|
117
|
+
|
118
|
+
specify "modifying the returned hash tree does not modify the ROS" do
|
119
|
+
subject.to_h[:blah][:blargh] = "Dr Scott"
|
120
|
+
|
121
|
+
subject.blah.blargh.should == "Janet"
|
122
|
+
end
|
115
123
|
end
|
116
124
|
|
117
|
-
it "does not mutate the
|
125
|
+
it "does not mutate the original hash tree passed to the constructor" do
|
118
126
|
hash[:blah][:blargh].should == 'Brad'
|
119
127
|
end
|
120
128
|
|
@@ -123,6 +131,22 @@ describe RecursiveOpenStruct do
|
|
123
131
|
|
124
132
|
hash[:some_array][0].should == 4
|
125
133
|
end
|
134
|
+
|
135
|
+
describe "#dup" do
|
136
|
+
let(:duped_subject) { subject.dup }
|
137
|
+
|
138
|
+
it "preserves sub-element modifications" do
|
139
|
+
duped_subject.blah.blargh.should == subject.blah.blargh
|
140
|
+
end
|
141
|
+
|
142
|
+
it "allows the copy's sub-elements to be modified independently from the original's" do
|
143
|
+
subject.blah.blargh.should == "Janet"
|
144
|
+
duped_subject.blah.blargh = "Dr. Scott"
|
145
|
+
|
146
|
+
duped_subject.blah.blargh.should == "Dr. Scott"
|
147
|
+
subject.blah.blargh.should == "Janet"
|
148
|
+
end
|
149
|
+
end
|
126
150
|
end
|
127
151
|
|
128
152
|
describe 'recursing over arrays' do
|
@@ -154,6 +178,12 @@ describe RecursiveOpenStruct do
|
|
154
178
|
}
|
155
179
|
end
|
156
180
|
|
181
|
+
it "deep-copies hashes within Arrays" do
|
182
|
+
subject.to_h[:blah][1][:foo] = "Rocky"
|
183
|
+
|
184
|
+
subject.blah[1].foo.should == "Dr Scott"
|
185
|
+
end
|
186
|
+
|
157
187
|
it "does not mutate the input hash passed to the constructor" do
|
158
188
|
h[:blah][1][:foo].should == '2'
|
159
189
|
end
|
@@ -161,6 +191,21 @@ describe RecursiveOpenStruct do
|
|
161
191
|
it "the deep copy recurses over Arrays as well" do
|
162
192
|
h[:blah][1][:foo].should == '2'
|
163
193
|
end
|
194
|
+
|
195
|
+
describe "#dup" do
|
196
|
+
let(:duped_subject) { subject.dup }
|
197
|
+
|
198
|
+
it "preserves sub-element modifications" do
|
199
|
+
duped_subject.blah[1].foo.should == subject.blah[1].foo
|
200
|
+
end
|
201
|
+
|
202
|
+
it "allows the copy's sub-elements to be modified independently from the original's" do
|
203
|
+
duped_subject.blah[1].foo = "Rocky"
|
204
|
+
|
205
|
+
duped_subject.blah[1].foo.should == "Rocky"
|
206
|
+
subject.blah[1].foo.should == "Dr Scott"
|
207
|
+
end
|
208
|
+
end
|
164
209
|
end
|
165
210
|
|
166
211
|
context "when array is nested deeper" do
|
data/spec/spec_helper.rb
CHANGED