From 0d3304dde6ec0851ace48f50d3a8f466150d697c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Thu, 9 Jul 2020 22:49:36 +0200 Subject: [PATCH] Add support for 'Gitlab::ObjectifiedHash#merge' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- lib/gitlab/objectified_hash.rb | 4 ++++ spec/gitlab/objectified_hash_spec.rb | 32 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/gitlab/objectified_hash.rb b/lib/gitlab/objectified_hash.rb index 2b70632c6..36d4cab0d 100644 --- a/lib/gitlab/objectified_hash.rb +++ b/lib/gitlab/objectified_hash.rb @@ -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}}" diff --git a/spec/gitlab/objectified_hash_spec.rb b/spec/gitlab/objectified_hash_spec.rb index 6b6d19d7c..08623e2ae 100644 --- a/spec/gitlab/objectified_hash_spec.rb +++ b/spec/gitlab/objectified_hash_spec.rb @@ -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}}"