class FactoryBotCaching::ImmutableIterator

Attributes

enumerator[R]

Public Class Methods

new(enumerable) click to toggle source
# File lib/factory_bot_caching/immutable_iterator.rb, line 27
def initialize(enumerable)
  # Make a shallow copy of the collection to ensure mutations to the original array do not affect our iterator:
  @enumerator = enumerable.is_a?(Enumerator) ? enumerable.to_a : enumerable.dup
  @position   = 0
end

Public Instance Methods

fast_forward(&comparison) click to toggle source
# File lib/factory_bot_caching/immutable_iterator.rb, line 47
def fast_forward(&comparison)
  while(next?)
    return if comparison.call(peek)
    advance_position
  end
end
next(&comparison) click to toggle source

Advances the position of the iterator to the next value. If a block is given, advances to the next value where the block returns true, or the end of the collection if none match.

# File lib/factory_bot_caching/immutable_iterator.rb, line 42
def next(&comparison)
  fast_forward(&comparison) if block_given?
  return next? ? next_value : nil
end
peek() click to toggle source
# File lib/factory_bot_caching/immutable_iterator.rb, line 54
def peek
  enumerator[@position]
end
until_end() { |next_value| ... } click to toggle source
# File lib/factory_bot_caching/immutable_iterator.rb, line 33
def until_end
  while(next?)
    yield next_value
  end
end

Private Instance Methods

advance_position() click to toggle source
# File lib/factory_bot_caching/immutable_iterator.rb, line 66
def advance_position
  @position += 1
end
next?() click to toggle source
# File lib/factory_bot_caching/immutable_iterator.rb, line 62
def next?
  @position < enumerator.count
end
next_value() click to toggle source
# File lib/factory_bot_caching/immutable_iterator.rb, line 70
def next_value
  peek
ensure
  advance_position
end