Class: Amazon::Util::PaginatedIterator
- Inherits:
-
Object
- Object
- Amazon::Util::PaginatedIterator
- Defined in:
- lib/amazon/util/paginated_iterator.rb
Overview
PaginatedIterator provides an iterator interface to a paginated dataset, buffering the current page. It can be used to stream large result sets which would not fit into memory or only need to be processed in a single pass.
Instance Attribute Summary collapse
-
#done ⇒ Object
readonly
Returns the value of attribute done.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
iterates over the remaining items.
-
#hasNext ⇒ Object
checks if we have another item available.
-
#initialize(&feeder) ⇒ PaginatedIterator
constructor
feeder should be a block that accepts a pagenumber and returns an array containing the corresponding page worth of results.
-
#next ⇒ Object
returns the next item, or nil if there are no more items.
-
#restart ⇒ Object
resets the iterator to start pulling from the first page.
Constructor Details
#initialize(&feeder) ⇒ PaginatedIterator
feeder should be a block that accepts a pagenumber and returns an array containing the corresponding page worth of results. It should return an empty array when there are no more results in the dataset.
17 18 19 20 |
# File 'lib/amazon/util/paginated_iterator.rb', line 17 def initialize( &feeder ) @feeder = feeder restart end |
Instance Attribute Details
#done ⇒ Object (readonly)
Returns the value of attribute done.
49 50 51 |
# File 'lib/amazon/util/paginated_iterator.rb', line 49 def done @done end |
Instance Method Details
#each(&block) ⇒ Object
iterates over the remaining items
42 43 44 45 46 47 |
# File 'lib/amazon/util/paginated_iterator.rb', line 42 def each( &block ) # :yields: item until @done item = self.next yield item unless item.nil? end end |
#hasNext ⇒ Object
checks if we have another item available
36 37 38 39 |
# File 'lib/amazon/util/paginated_iterator.rb', line 36 def hasNext fetchpage if @buffer.empty? return !@buffer.empty? end |
#next ⇒ Object
returns the next item, or nil if there are no more items
30 31 32 33 |
# File 'lib/amazon/util/paginated_iterator.rb', line 30 def next fetchpage if @buffer.empty? @buffer.shift end |
#restart ⇒ Object
resets the iterator to start pulling from the first page
23 24 25 26 27 |
# File 'lib/amazon/util/paginated_iterator.rb', line 23 def restart @buffer = [] @page = 1 @done = false end |