Class: Mongoid::Collections::CyclicIterator

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/collections/cyclic_iterator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ CyclicIterator

Performs iteration over an array, if the array gets to the end then loop back to the first.

Example:

CyclicIterator.new([ first, second ])



14
15
16
# File 'lib/mongoid/collections/cyclic_iterator.rb', line 14

def initialize(array)
  @array, @counter = array, -1
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



6
7
8
# File 'lib/mongoid/collections/cyclic_iterator.rb', line 6

def counter
  @counter
end

Instance Method Details

#nextObject

Get the next element in the array. If the element is the last in the array then return the first.

Example:

iterator.next

Returns:

The next element in the array.



28
29
30
31
# File 'lib/mongoid/collections/cyclic_iterator.rb', line 28

def next
  (@counter == @array.size - 1) ? @counter = 0 : @counter = @counter + 1
  @array[@counter]
end