Class: Kiss::Iterator
Overview
Enables ‘loop’ features for certain template loops (while, for…in). Similar to features of Perl’s Template::Iterator by Andy Wardley.
Instance Method Summary collapse
-
#count ⇒ Object
Return current iteration number, indexed from one instead of zero.
-
#first? ⇒ Boolean
(also: #first)
Returns true if this is loop’s first iteration.
-
#increment ⇒ Object
Used by template erubis pre-processing voodoo to advance the iterator index.
-
#initialize(collection = nil) ⇒ Iterator
constructor
Creates a new loop iterator.
-
#last? ⇒ Boolean
(also: #last)
Returns true if this is the last iteration of a loop over a collection.
-
#max ⇒ Object
Returns index of loop’s last iteration.
-
#next ⇒ Object
Return next item in loop-iterated collection.
-
#prev ⇒ Object
Return previous item in loop-iterated collection.
-
#size ⇒ Object
Returns number of iterations in loop over a collection (size of the iterated collection).
Constructor Details
#initialize(collection = nil) ⇒ Iterator
Creates a new loop iterator.
8 9 10 11 |
# File 'lib/kiss/iterator.rb', line 8 def initialize(collection = nil) @_collection = collection @_index = -1 end |
Instance Method Details
#count ⇒ Object
Return current iteration number, indexed from one instead of zero.
14 15 16 |
# File 'lib/kiss/iterator.rb', line 14 def count @_index + 1 end |
#first? ⇒ Boolean Also known as: first
Returns true if this is loop’s first iteration.
19 20 21 |
# File 'lib/kiss/iterator.rb', line 19 def first? @_index == 0 end |
#increment ⇒ Object
Used by template erubis pre-processing voodoo to advance the iterator index. Not intended for any other use.
54 55 56 |
# File 'lib/kiss/iterator.rb', line 54 def increment @_index = @_index + 1 end |
#last? ⇒ Boolean Also known as: last
Returns true if this is the last iteration of a loop over a collection.
26 27 28 |
# File 'lib/kiss/iterator.rb', line 26 def last? count == size end |
#max ⇒ Object
Returns index of loop’s last iteration.
42 43 44 |
# File 'lib/kiss/iterator.rb', line 42 def max @_collection ? @_collection.size-1 : nil end |
#next ⇒ Object
Return next item in loop-iterated collection.
37 38 39 |
# File 'lib/kiss/iterator.rb', line 37 def next @_collection ? @_collection[index+1] : nil end |
#prev ⇒ Object
Return previous item in loop-iterated collection.
32 33 34 |
# File 'lib/kiss/iterator.rb', line 32 def prev @_collection ? @_collection[index-1] : nil end |
#size ⇒ Object
Returns number of iterations in loop over a collection (size of the iterated collection).
48 49 50 |
# File 'lib/kiss/iterator.rb', line 48 def size @_collection ? @_collection.size : nil end |