Skip to content
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

Add support for 'Gitlab::ObjectifiedHash#merge' #576

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/gitlab/objectified_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def to_hash
end
alias to_h to_hash

def merge(other_hash)
self.class.new(to_h.merge(other_hash))
end

# @return [String] Formatted string with the class name, object id and original hash.
def inspect
"#<#{self.class}:#{object_id} {hash: #{hash.inspect}}"
Expand Down
32 changes: 32 additions & 0 deletions spec/gitlab/objectified_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,38 @@
end
end

describe '#merge' do
before do
@hash2 = { c: 3 }
end

shared_examples 'mergeable ObjectifiedHash' do
it 'returns a ObjectifiedHash' do
expect(@merged_oh).to be_a(described_class)
end

it 'merges two ObjectifiedHash objects' do
expect(@merged_oh.to_h).to eq(@hash.merge(@hash2))
end
end

context 'when merging two ObjectifiedHash objects' do
before do
@merged_oh = @oh.merge(described_class.new(@hash2))
end

it_behaves_like 'mergeable ObjectifiedHash'
end

context 'when merging one ObjectifiedHash and a hash' do
before do
@merged_oh = @oh.merge(@hash2)
end

it_behaves_like 'mergeable ObjectifiedHash'
end
end

describe '#inspect' do
it 'returns a formatted string' do
pretty_string = "#<#{@oh.class.name}:#{@oh.object_id} {hash: #{@hash}}"
Expand Down