8000 Add merge functionality by davich · Pull Request #44 · aetherknight/recursive-open-struct · GitHub
[go: up one dir, main page]

Skip to content

Add merge functionality #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/recursive_open_struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
13 changes: 13 additions & 0 deletions spec/recursive_open_struct/recursion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
0