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

Fix for calling #flatten on an array of links #37

Merged
merged 1 commit into from
Jun 23, 2013
Merged
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
8 changes: 8 additions & 0 deletions lib/hyperclient/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion test/hyperclient/link_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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