recursive-open-struct 0.6.0 → 0.6.1

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
  SHA1:
3
- metadata.gz: 72ff48c134d3b90f56c0c2808e55f107c8bdd302
4
- data.tar.gz: 3acf497ceb23b7b86de443dc3b4a83ddffa143eb
3
+ metadata.gz: ad6b86b4a8ed2c69543135a33097d06f13e92671
4
+ data.tar.gz: bb738b78cb40ff95bb7d257cda73a68b5dec6f7b
5
5
  SHA512:
6
- metadata.gz: 0279ca781f0607ae71c16d21589644a7b6151605f7f8cd74b47129ff2aad4c99536893375215aa9308342db54ba117ce271346bbc9c6a589c10e0f5bb105c9d1
7
- data.tar.gz: be2d8df65e43686494858e8ab4cbec822336bcd5f68d0f87445fd968afd4ea08770e766192c489d900ae7d6b7233b9a17308da0089c7f6aa821b45e967f29a4c
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
@@ -1,6 +1,8 @@
1
1
  source 'http://rubygems.org'
2
2
  gemspec
3
3
 
4
+ gem 'pry'
5
+
4
6
  group :development do
5
7
  if RUBY_VERSION =~ /^1\.8/
6
8
  gem 'rcov'
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
- ros = RecursiveOpenStruct.new( { :fooa => { :foob => 'fooc' } } )
8
+ ros = RecursiveOpenStruct.new( { :fooa => { :foob => 'fooc' } } )
9
9
 
10
- ros.fooa.foob # => 'fooc'
10
+ ros.fooa.foob # => 'fooc'
11
11
 
12
12
  Also, if needed, nested hashes can still be accessed as hashes:
13
13
 
14
- ros.fooa_as_a_hash # { :foob => 'fooc' }
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
- h = { :somearr => [ { :name => 'a'}, { :name => 'b' } ] }
19
+ h = { :somearr => [ { :name => 'a'}, { :name => 'b' } ] }
20
20
 
21
- ros = RecursiveOpenStruct.new(h, :recurse_over_arrays => true )
21
+ ros = RecursiveOpenStruct.new(h, :recurse_over_arrays => true )
22
22
 
23
- ros.somarr[0].name # => 'a'
24
- ros.somarr[1].name # => 'b'
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
- gem 'recursive-open-struct'
32
+ gem 'recursive-open-struct'
33
33
 
34
34
  You may also install the gem manually :
35
35
 
36
- gem install recursive-open-struct
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
- @table.dup.update(@sub_elements) do |k, oldval, newval|
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
@@ -3,5 +3,5 @@
3
3
  require 'ostruct'
4
4
 
5
5
  class RecursiveOpenStruct < OpenStruct
6
- VERSION = "0.6.0"
6
+ VERSION = "0.6.1"
7
7
  end
@@ -110,11 +110,19 @@ describe RecursiveOpenStruct do
110
110
 
111
111
  before(:each) { subject.blah.blargh = "Janet" }
112
112
 
113
- it "returns a hash tree that contains those modifications" do
114
- subject.to_h.should == updated_hash
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 input hash tree passed to the constructor" do
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
@@ -1,6 +1,7 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  require 'rspec'
4
+ require 'pry'
4
5
 
5
6
  if ENV['COVERAGE'] == 'true'
6
7
  require 'simplecov'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recursive-open-struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - William (B.J.) Snow Orvis