Class: Seq::Paged
Overview
Paged seqs evaluate a block that returns a page (Array) of items that are then returned one at a time. This is useful for working with web services that return pages of results, when you need them as a list.
Constant Summary
Constants inherited from Seq
Instance Method Summary collapse
-
#ended? ⇒ Boolean
Whether the Paged seq has returned all of its items.
-
#initialize(offset = 0, default = nil) {|page| ... } ⇒ Paged
constructor
Creates a new Paged seq instance.
-
#next ⇒ Object
Until ended it return the next item in the paged list.
-
#reset ⇒ Object
Resets the state of the paged seq.
Methods inherited from Seq
#each, #entries, #inc, #infinite?, #method_missing, #to_a
Constructor Details
#initialize(offset = 0, default = nil) {|page| ... } ⇒ Paged
Creates a new Paged seq instance.
33 34 35 36 37 38 39 |
# File 'lib/seq/paged.rb', line 33 def initialize(offset=0, default=nil, &block) @block = block @offset = offset @default = default self.reset end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Seq
Instance Method Details
#ended? ⇒ Boolean
55 56 57 |
# File 'lib/seq/paged.rb', line 55 def ended? @index >= @items.size && @done end |
#next ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/seq/paged.rb', line 61 def next return @default if ended? while @items.size <= @index loaded = @block.call(@page) @page += 1 if loaded.empty? @done = true return @default end @items += loaded end item = @items[@index] @index += 1 item end |
#reset ⇒ Object
Resets the state of the paged seq. It also calculates any values necessary to get to the offset.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/seq/paged.rb', line 43 def reset @index = 0 @page = 0 @done = false @items = [] until @index >= @offset self.next end end |