Skip to content

Commit c116df7

Browse files
committed
Reapply "Merge pull request #72 from wildmaples/fix-marshal-load-issue"
This reverts commit 49265a8.
1 parent cc1adb4 commit c116df7

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/recursive_open_struct.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ def initialize(hash=nil, passed_options={})
4040
@sub_elements = {}
4141
end
4242

43+
def marshal_load(attributes)
44+
hash, @options = attributes
45+
@deep_dup = DeepDup.new(@options)
46+
@sub_elements = {}
47+
super(hash)
48+
end
49+
50+
def marshal_dump
51+
[super, @options]
52+
end
4353

4454
if OpenStruct.public_instance_methods.include?(:initialize_copy)
4555
def initialize_copy(orig)

spec/recursive_open_struct/recursion_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
expect(ros.blah.changed).to eql 'backing'
3838
end
3939

40+
it "handles being dump then loaded by Marshal" do
41+
foo_struct = [RecursiveOpenStruct.new]
42+
bar_struct = RecursiveOpenStruct.new(foo: foo_struct)
43+
serialized = Marshal.dump(bar_struct)
44+
45+
expect(Marshal.load(serialized).foo).to eq(foo_struct)
46+
end
47+
4048
describe "handling loops in the original Hashes" do
4149
let(:h1) { { :a => 'a'} }
4250
let(:h2) { { :a => 'b', :h1 => h1 } }
@@ -182,6 +190,17 @@
182190
let(:blah_list) { [ { :foo => '1' }, { :foo => '2' }, 'baz' ] }
183191
let(:h) { { :blah => blah_list } }
184192

193+
context "when dump and loaded by Marshal" do
194+
let(:test) { RecursiveOpenStruct.new(h, :recurse_over_arrays => true) }
195+
subject { Marshal.load(Marshal.dump(test))}
196+
197+
it { expect(subject.blah.length).to eq 3 }
198+
it { expect(subject.blah[0].foo).to eq '1' }
199+
it { expect(subject.blah[1].foo).to eq '2' }
200+
it { expect(subject.blah_as_a_hash).to eq blah_list }
201+
it { expect(subject.blah[2]).to eq 'baz' }
202+
end
203+
185204
context "when recursing over arrays is enabled" do
186205
subject { RecursiveOpenStruct.new(h, :recurse_over_arrays => true) }
187206

0 commit comments

Comments
 (0)