Class: Amazon::Util::LazyResults
- Inherits:
-
Object
- Object
- Amazon::Util::LazyResults
- Includes:
- Enumerable
- Defined in:
- lib/amazon/util/lazy_results.rb
Overview
This class provides a wrapper for lazy evaluation of results. The constructor takes a block which should accept a pagenumber and return a page worth of results.
Instance Method Summary collapse
-
#[](index) ⇒ Object
index into the array set.
-
#each(&block) ⇒ Object
iterate over entire result set, loading lazily.
-
#flush ⇒ Object
clear the result set and start over again.
-
#initialize(&feeder) ⇒ LazyResults
constructor
A new instance of LazyResults.
-
#inspect ⇒ Object
:nodoc:.
-
#to_a ⇒ Object
fully populate the result set and return a true array.
Constructor Details
#initialize(&feeder) ⇒ LazyResults
Returns a new instance of LazyResults.
15 16 17 18 |
# File 'lib/amazon/util/lazy_results.rb', line 15 def initialize( &feeder ) @iterator = PaginatedIterator.new( &feeder ) flush end |
Instance Method Details
#[](index) ⇒ Object
index into the array set. if requested index has not been loaded, will load up to that index
33 34 35 36 |
# File 'lib/amazon/util/lazy_results.rb', line 33 def []( index ) feedme while !@iterator.done and index >= @truth.size return @truth[index] end |
#each(&block) ⇒ Object
iterate over entire result set, loading lazily
27 28 29 30 |
# File 'lib/amazon/util/lazy_results.rb', line 27 def each( &block ) # :yields: item @truth.each {|e| yield e } @iterator.each {|e| @truth << e ; yield e } end |
#flush ⇒ Object
clear the result set and start over again
21 22 23 24 |
# File 'lib/amazon/util/lazy_results.rb', line 21 def flush @truth = [] @iterator.restart end |
#inspect ⇒ Object
:nodoc:
44 45 46 |
# File 'lib/amazon/util/lazy_results.rb', line 44 def inspect # :nodoc: "#<Amazon::Util::LazyResults truth_size=#{@truth.size} page=#{@page} done=#{@done}>" end |
#to_a ⇒ Object
fully populate the result set and return a true array
39 40 41 42 |
# File 'lib/amazon/util/lazy_results.rb', line 39 def to_a feedme until @iterator.done return @truth.dup end |