Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
PageableResponse no longer enumerates all pages in response to #count.
Browse files Browse the repository at this point in the history
Fixes #106
  • Loading branch information
trevorrowe committed Oct 17, 2014
1 parent 086e557 commit c7ba435
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Next Release (TBD)
------------------

* Issue - Aws::PageableResponse - Calling `#count` on a pageable response
was triggering n API calls to get the number of response pages. Instead
it raises an error now, unless the wrapped data object responds to
count. Fixes #106.

2.0.3 (2014-10-16)
------------------

Expand Down
18 changes: 18 additions & 0 deletions aws-sdk-core/lib/aws-sdk-core/pageable_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ def each_page(&block)
end
alias each each_page

# @api private
def count
if respond_to?(:count)
data.count
else
raise NotImplementedError
end
end

# @api private
def respond_to?(method_name, *args)
if method_name == :count
data.respond_to?(:count)
else
super
end
end

private

# @param [Hash] params A hash of additional request params to
Expand Down
34 changes: 34 additions & 0 deletions aws-sdk-core/spec/aws/pageable_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,39 @@ module Aws
end

end

describe '#count' do

it 'raises not implemented error by default' do
data = double('data')
resp = double('resp', data:data, error:nil, context:nil)
page = PageableResponse.new(resp, Paging::NullPager.new)
expect {
page.count
}.to raise_error(NotImplementedError)
end

it 'passes count from the raises not implemented error by default' do
data = double('data', count: 10)
resp = double('resp', data:data, error:nil, context:nil)
page = PageableResponse.new(resp, Paging::NullPager.new)
expect(page.count).to eq(10)
end

it 'returns false from respond_to when count not present' do
data = double('data')
resp = double('resp', data:data, error:nil, context:nil)
page = PageableResponse.new(resp, Paging::NullPager.new)
expect(page.respond_to?(:count)).to be(false)
end

it 'indicates it responds to count when data#count exists' do
data = double('data', count: 10)
resp = double('resp', data:data, error:nil, context:nil)
page = PageableResponse.new(resp, Paging::NullPager.new)
expect(page.respond_to?(:count))
end

end
end
end

0 comments on commit c7ba435

Please sign in to comment.