From ac4bf0fcb3abaacb416adc66f44ed3663b3c1f23 Mon Sep 17 00:00:00 2001 From: David Carlin Date: Mon, 16 May 2016 13:37:07 +1000 Subject: [PATCH] Add merge functionality --- lib/recursive_open_struct.rb | 4 ++++ spec/recursive_open_struct/recursion_spec.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/recursive_open_struct.rb b/lib/recursive_open_struct.rb index b147221..d941d0c 100644 --- a/lib/recursive_open_struct.rb +++ b/lib/recursive_open_struct.rb @@ -42,6 +42,10 @@ def [](name) public_send(name) end + def merge(other) + self.class.new(self.to_h.merge(other.to_h)) + end + # Makes sure ROS responds as expected on #respond_to? and #method requests def respond_to_missing?(mid, include_private = false) mname = _get_key_from_table_(mid.to_s.chomp('=').chomp('_as_a_hash')) diff --git a/spec/recursive_open_struct/recursion_spec.rb b/spec/recursive_open_struct/recursion_spec.rb index 168b51d..34bb1c6 100644 --- a/spec/recursive_open_struct/recursion_spec.rb +++ b/spec/recursive_open_struct/recursion_spec.rb @@ -16,6 +16,19 @@ expect(ros.to_hash).to eq h end + describe "#merge" do + let(:other_h) { { more: 'data' } } + let(:other_ros) { RecursiveOpenStruct.new(other_h) } + + it "can merge in a hash" do + expect(ros.merge(other_h).to_h).to eq(h.merge(other_h)) + end + + it "can merge in a RecursiveOpenStruct" do + expect(ros.merge(other_ros).to_h).to eq(h.merge(other_h)) + end + end + it "returns accessed hashes as RecursiveOpenStructs instead of hashes" do expect(subject.blah.another).to eq 'value' end