diff --git a/lib/gitlab/objectified_hash.rb b/lib/gitlab/objectified_hash.rb index 2b70632c6..e2f12c514 100644 --- a/lib/gitlab/objectified_hash.rb +++ b/lib/gitlab/objectified_hash.rb @@ -34,7 +34,14 @@ def [](key) # Respond to messages for which `self.data` has a key def method_missing(method_name, *args, &block) - data.key?(method_name.to_s) ? data[method_name.to_s] : super + if data.key?(method_name.to_s) + data[method_name.to_s] + elsif data.respond_to?(method_name) + warn 'WARNING: Please convert ObjectifiedHash object to hash before calling Hash methods on it.' + data.send(method_name, *args, &block) + else + super + end end def respond_to_missing?(method_name, include_private = false) diff --git a/spec/gitlab/objectified_hash_spec.rb b/spec/gitlab/objectified_hash_spec.rb index 6b6d19d7c..9b96cfc65 100644 --- a/spec/gitlab/objectified_hash_spec.rb +++ b/spec/gitlab/objectified_hash_spec.rb @@ -8,6 +8,21 @@ @oh = described_class.new @hash end + describe 'Hash behavior' do + let(:hash) { { foo: 'bar' } } + let(:oh) { described_class.new(hash) } + + it 'allows to call Hash methods' do + expect(oh.dig('foo')).to eq('bar') + expect(oh.merge(key: :value)).to eq({ 'foo' => 'bar', key: :value }) + end + + it 'warns about calling Hash methods' do + output = capture_output { oh.values } + expect(output).to eq("WARNING: Please convert ObjectifiedHash object to hash before calling Hash methods on it.\n") + end + end + it 'objectifies a hash' do expect(@oh.a).to eq(@hash[:a]) expect(@oh.b).to eq(@hash[:b])