Skip to content

Commit 2333ddd

Browse files
authored
Merge pull request #319 from dblock/dblock/fix-resource-to-h
Fix Resource#to_h returning nil instead of matching #to_hash
2 parents 04522e1 + f47b4fe commit 2333ddd

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [#298](https://github.com/codegram/hyperclient/pull/300): Upgraded RuboCop to 1.80.2 - [@dblock](https://github.com/dblock).
66
* [#298](https://github.com/codegram/hyperclient/pull/298): Upgraded RuboCop to 1.63.5 - [@dblock](https://github.com/dblock).
7+
* [#319](https://github.com/codegram/hyperclient/pull/319): Fixed `Resource#to_h` returning `nil` instead of the same result as `#to_hash` - [@dblock](https://github.com/dblock).
78
* [#320](https://github.com/codegram/hyperclient/pull/320): Documented how to handle non-`hal+json` responses via the Faraday response middleware's `content_type` matcher - [@dblock](https://github.com/dblock).
89
* [#321](https://github.com/codegram/hyperclient/pull/321): Added Ruby 4.0 to the CI test matrix - [@dblock](https://github.com/dblock).
910
* Your contribution here.

‎lib/hyperclient/resource.rb‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ def [](name)
6666
send(name) if respond_to?(name)
6767
end
6868

69+
# Public: Returns the attributes of the Resource as a Hash.
70+
#
71+
# Delegates explicitly (rather than relying on method_missing) since
72+
# Array also defines #to_h (but not #to_hash), which meant the
73+
# `Array.method_defined?(method)` guard in method_missing let #to_hash
74+
# through but silently swallowed #to_h, returning nil instead of the
75+
# attributes hash.
76+
def to_h
77+
_attributes.to_h
78+
end
79+
alias to_hash to_h
80+
6981
def fetch(key, *args)
7082
return self[key] if respond_to?(key)
7183

‎test/hyperclient/collection_test.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ module Hyperclient
4141
end
4242
end
4343

44+
describe '#to_h' do
45+
it 'returns the wrapped collection as a hash' do
46+
_(collection.to_h).must_be_kind_of Hash
47+
end
48+
49+
it 'returns the same result as #to_hash' do
50+
_(collection.to_h).must_equal(collection.to_hash)
51+
end
52+
end
53+
4454
describe '#to_s' do
4555
it 'returns the wrapped collection as a hash' do
4656
_(collection.to_s).must_be_kind_of Hash

‎test/hyperclient/resource_test.rb‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,5 +239,19 @@ module Hyperclient
239239
_(resource.inspect).must_include 'attributes:'
240240
end
241241
end
242+
243+
describe '#to_h and #to_hash' do
244+
let(:resource) do
245+
Resource.new({ '_links' => {}, 'title' => 'Order', 'total' => 42 }, entry_point)
246+
end
247+
248+
it 'returns the attributes as a Hash from #to_hash' do
249+
_(resource.to_hash).must_equal('title' => 'Order', 'total' => 42)
250+
end
251+
252+
it 'returns the same result from #to_h as #to_hash' do
253+
_(resource.to_h).must_equal(resource.to_hash)
254+
end
255+
end
242256
end
243257
end

0 commit comments

Comments
 (0)