Class: MTK::Patterns::Chain

Inherits:
Pattern
  • Object
show all
Defined in:
lib/mtk/patterns/chain.rb

Overview

A pattern that takes a list of patterns and combines their values into a list of event properties.

Instance Attribute Summary

Attributes inherited from Pattern

#cycle_count, #element_count, #elements, #max_cycles, #max_elements, #min_elements, #options

Instance Method Summary collapse

Methods inherited from Pattern

#empty?, from_a, #max_cycles_exceeded?, #max_elements_exceeded?, #min_elements_unmet?, #next, #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

#initialize(elements, options = {}) ⇒ Chain

Returns a new instance of Chain.



7
8
9
10
11
# File 'lib/mtk/patterns/chain.rb', line 7

def initialize(elements, options={})
  super
  @stop_after_first = true # assume everything's an element until we inspect them in the loop below
  @stop_after_first = true if @elements.all?{|element| not element.is_a? ::MTK::Patterns::Pattern }
end

Instance Method Details

#advanceObject (protected)

Update internal state (index, etc) and set @current to the next element.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mtk/patterns/chain.rb', line 23

def advance
  @current = @elements.map.with_index do |element,index|
    if element.is_a? ::MTK::Patterns::Pattern
      begin
        element.next
      rescue StopIteration
        raise StopIteration if element.max_elements_exceeded?
        @is_element_done[index] = true
        element.rewind
        element.next
      end
    else
      element
    end
  end.flatten

  raise StopIteration if @is_element_done.all?{|done| done }

  @elements.each.with_index do |element,index|
    @is_element_done[index] = true unless element.is_a? ::MTK::Patterns::Pattern
  end
end

#rewind_or_cycle(is_cycling = false) ⇒ Object (protected)

Reset the pattern to the beginning

Parameters:

  • is_cycling (Boolean) (defaults to: false)

    true when #next is performing a cycle back to the beginning of the Pattern. false for a normal #rewind



17
18
19
20
# File 'lib/mtk/patterns/chain.rb', line 17

def rewind_or_cycle(is_cycling=false)
  @is_element_done = Array.new(elements.size)
  super
end