Class: MTK::Patterns::ForEach
- Defined in:
- lib/mtk/patterns/for_each.rb
Overview
For each value in the first sub-pattern, iterate over the second sub-pattern and chain the resulting values.
Instance Attribute Summary
Attributes inherited from Pattern
#cycle_count, #element_count, #elements, #max_cycles, #max_elements, #min_elements, #options
Instance Method Summary collapse
-
#next ⇒ Object
Emit the next element in the pattern.
-
#rewind_or_cycle(is_cycling = false) ⇒ Object
protected
Reset the pattern to the beginning.
Methods inherited from Pattern
#advance, #empty?, from_a, #initialize, #max_cycles_exceeded?, #max_elements_exceeded?, #min_elements_unmet?, #rewind
Methods included from Groups::Collection
#==, #[], #clone, #concat, #each, #empty?, #enumerable_map, #first, #last, #map, #partition, #permute, #repeat, #reverse, #rotate, #size, #to_a
Constructor Details
This class inherits a constructor from MTK::Patterns::Pattern
Instance Method Details
#next ⇒ Object
Emit the next element in the pattern
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mtk/patterns/for_each.rb', line 9 def next @index = 0 if @index < 0 last_index = @elements.length-1 while @index <= last_index # assume all elements are Patterns, otherwise this construct doesn't really have a point... pattern = @elements[@index] begin element = pattern.next value = evaluate_variables(element) if @index == last_index # then emit values @current = value return emit(value) else # not last element, so store variables @vars.push value @index += 1 end rescue StopIteration if @index==0 raise # We're done when the first pattern is done else pattern.rewind @vars.pop @index -= 1 end end end end |
#rewind_or_cycle(is_cycling = false) ⇒ Object (protected)
Reset the pattern to the beginning
46 47 48 49 50 |
# File 'lib/mtk/patterns/for_each.rb', line 46 def rewind_or_cycle(is_cycling=false) @vars = [] @elements.each{|elem| elem.rewind } super end |