Sha256: 3566c8280e524e173b4655e0f9c09c885b40f2c3eb5cef1af7f5062c5070ea22
Contents?: true
Size: 1.46 KB
Versions: 14
Compression:
Stored size: 1.46 KB
Contents
class Kiss # Enables `loop' features for certain template loops (while, for...in). # Similar to features of Perl's Template::Iterator by Andy Wardley. class Iterator _attr_reader :index, :collection # Creates a new loop iterator. def initialize(collection = nil) @_collection = collection @_index = -1 end # Return current iteration number, indexed from one instead of zero. def count @_index + 1 end # Returns true if this is loop's first iteration. def first? @_index == 0 end alias_method :first, :first? # Returns true if this is the last iteration of a # loop over a collection. def last? count == size end alias_method :last, :last? # Return previous item in loop-iterated collection. def prev @_collection ? @_collection[index-1] : nil end # Return next item in loop-iterated collection. def next @_collection ? @_collection[index+1] : nil end # Returns index of loop's last iteration. def max @_collection ? @_collection.size-1 : nil end # Returns number of iterations in loop over a collection # (size of the iterated collection). def size @_collection ? @_collection.size : nil end # Used by template erubis pre-processing voodoo to advance # the iterator index. Not intended for any other use. def increment @_index = @_index + 1 end end end
Version data entries
14 entries across 14 versions & 1 rubygems