Skip to content

Commit c569909

Browse files
committed
Dump and load options and sub_elements
Previously, ROS did not play well when being dump and loaded with Marshal because the options from ROS did not translate through the serialization. This change saves the information through marshal_dump and then reassigns the information to ROS instance when the object is loaded.
1 parent da38709 commit c569909

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/recursive_open_struct.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ def initialize(hash=nil, passed_options={})
3939
@sub_elements = {}
4040
end
4141

42+
def marshal_load(attributes)
43+
hash, @options, @sub_elements = attributes
44+
super(hash)
45+
end
46+
47+
def marshal_dump
48+
[super, @options, @sub_elements]
49+
end
4250

4351
if OpenStruct.public_instance_methods.include?(:initialize_copy)
4452
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)