diff --git a/lib/hyperclient/link.rb b/lib/hyperclient/link.rb index 0bb08fa..8b692ab 100644 --- a/lib/hyperclient/link.rb +++ b/lib/hyperclient/link.rb @@ -104,6 +104,14 @@ def method_missing(method, *args, &block) def respond_to_missing?(method, include_private = false) resource.respond_to?(method.to_s) end + + # Internal: avoid delegating to resource + # + # #to_ary is called for implicit array coersion (such as parallel assignment + # or Array#flatten). Returning nil tells Ruby that this record is not Array-like. + def to_ary + nil + end end # Public: Exception that is raised when building a templated Link without uri diff --git a/test/hyperclient/link_test.rb b/test/hyperclient/link_test.rb index 0167617..188120d 100644 --- a/test/hyperclient/link_test.rb +++ b/test/hyperclient/link_test.rb @@ -149,13 +149,14 @@ module Hyperclient before do stub_request(:get, "http://myapi.org/orders"). to_return(body: '{"resource": "This is the resource"}') - Resource.expects(:new).returns(resource).at_least_once + Resource.stubs(:new).returns(resource) end let(:link) { Link.new({'href' => 'http://myapi.org/orders'}, entry_point) } let(:resource) { mock('Resource') } it 'delegates unkown methods to the resource' do + Resource.expects(:new).returns(resource).at_least_once resource.expects(:embedded) link.embedded @@ -169,6 +170,11 @@ module Hyperclient resource.expects(:respond_to?).with('embedded').returns(true) link.respond_to?(:embedded).must_equal true end + + it 'does not delegate to_ary to resource' do + resource.expects(:to_ary).never + [[link, link]].flatten.must_equal [link, link] + end end end end