Class: Seq
- Inherits:
-
Object
- Object
- Seq
- Includes:
- Enumerable
- Defined in:
- lib/seq.rb,
lib/seq/lazy.rb,
lib/seq/paged.rb,
lib/seq/pager.rb,
lib/seq/random.rb,
lib/seq/version.rb
Overview
A Seq will cycle over a list returning a certain number of items.
Defined Under Namespace
Classes: Lazy, Paged, Pager, Random
Constant Summary collapse
- VERSION =
"0.3.0"
Instance Method Summary collapse
-
#each ⇒ Object
Iterates over each item as returned by #next until #ended?.
-
#ended? ⇒ Boolean
Whether the Seq has returned enough items.
-
#entries ⇒ Array
The items that would be returned by repeated calls to #next until #ended?.
-
#inc ⇒ Integer
Increment the list index, the number of cycles completed and if at the end of the list returns to the first item.
-
#infinite? ⇒ Boolean
Whether the Seq returns infinite items.
-
#initialize(list = [], items = Float::INFINITY, offset = 0, default = nil) ⇒ Seq
constructor
Creates a new instance of Seq.
-
#method_missing(sym, *args, &block) ⇒ Object
Any method called on a Seq with ending with !, will be caught by method missing, it will then attempt to call the non ! version but will not alter the index or number of cycles completed.
-
#next ⇒ Object
Until ended it returns the next item from the list, when ended it returns the default item.
-
#reset ⇒ Object
Resets the Seqs position to the same as when initialized.
-
#to_a ⇒ Array
Returns the original list.
Constructor Details
#initialize(list = [], items = Float::INFINITY, offset = 0, default = nil) ⇒ Seq
Creates a new instance of Seq.
43 44 45 46 47 48 49 50 |
# File 'lib/seq.rb', line 43 def initialize(list=[], items=Float::INFINITY, offset=0, default=nil) @list = list @items = items @offset = offset @default = default self.reset end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Any method called on a Seq with ending with !, will be caught by method missing, it will then attempt to call the non ! version but will not alter the index or number of cycles completed.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/seq.rb', line 120 def method_missing(sym, *args, &block) if sym.to_s[-1] == "!" && self.respond_to?(sym.to_s[0..-2].to_sym) && ! [:infinite?, :ended?, :to_a, :entries, :inc, :reset].include?(sym.to_s[0..-2].to_sym) begin i, c = @index, @cycles self.send(sym.to_s[0..-2].to_sym, *args, &block) ensure @index, @cycles = i, c end else super end end |
Instance Method Details
#each ⇒ Object
Iterates over each item as returned by #next until #ended?.
82 83 84 85 86 |
# File 'lib/seq.rb', line 82 def each until ended? yield self.next end end |
#ended? ⇒ Boolean
Returns Whether the Seq has returned enough items.
113 114 115 |
# File 'lib/seq.rb', line 113 def ended? @cycles >= @items end |
#entries ⇒ Array
Returns The items that would be returned by repeated calls to #next until #ended?.
93 94 95 96 97 98 99 100 |
# File 'lib/seq.rb', line 93 def entries raise RangeError if infinite? i, c = @index, @cycles r = super @index, @cycles = i, c r end |
#inc ⇒ Integer
Increment the list index, the number of cycles completed and if at the end of the list returns to the first item.
72 73 74 75 76 77 78 79 |
# File 'lib/seq.rb', line 72 def inc if @index+1 == @list.size @index = 0 else @index += 1 end @cycles += 1 end |
#infinite? ⇒ Boolean
Returns Whether the Seq returns infinite items.
108 109 110 |
# File 'lib/seq.rb', line 108 def infinite? @items == Float::INFINITY end |
#next ⇒ Object
Returns Until ended it returns the next item from the list, when ended it returns the default item.
60 61 62 63 64 65 66 |
# File 'lib/seq.rb', line 60 def next if ended? @default else @list[@index].tap { inc } end end |
#reset ⇒ Object
Resets the Seqs position to the same as when initialized.
53 54 55 56 |
# File 'lib/seq.rb', line 53 def reset @cycles = 0 @index = @offset end |
#to_a ⇒ Array
Returns the original list
103 104 105 |
# File 'lib/seq.rb', line 103 def to_a @list end |