Class: FactoryBotCaching::ImmutableIterator
- Inherits:
-
Object
- Object
- FactoryBotCaching::ImmutableIterator
- Defined in:
- lib/factory_bot_caching/immutable_iterator.rb
Instance Method Summary collapse
- #fast_forward(&comparison) ⇒ Object
-
#initialize(enumerable) ⇒ ImmutableIterator
constructor
A new instance of ImmutableIterator.
-
#next(&comparison) ⇒ Object
Advances the position of the iterator to the next value.
- #peek ⇒ Object
- #until_end ⇒ Object
Constructor Details
#initialize(enumerable) ⇒ ImmutableIterator
Returns a new instance of ImmutableIterator.
27 28 29 30 31 |
# 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 |
Instance Method Details
#fast_forward(&comparison) ⇒ Object
47 48 49 50 51 52 |
# 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) ⇒ Object
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.
42 43 44 45 |
# 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 ⇒ Object
54 55 56 |
# File 'lib/factory_bot_caching/immutable_iterator.rb', line 54 def peek enumerator[@position] end |
#until_end ⇒ Object
33 34 35 36 37 |
# File 'lib/factory_bot_caching/immutable_iterator.rb', line 33 def until_end while(next?) yield next_value end end |