Class: MTK::Patterns::Function

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

Overview

An arbitrary function that dynamically generates elements.

Instance Attribute Summary collapse

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 = {}) ⇒ Function

Returns a new instance of Function.



10
11
12
13
14
# File 'lib/mtk/patterns/function.rb', line 10

def initialize(elements, options={})
  # unpack from the varargs Array that may be passed in from the "convenience constructor methods" defined in MTK::Pattern                        \
  @function = if elements.is_a? Enumerable then elements.first else elements end
  super [@function], options
end

Instance Attribute Details

#functionObject (readonly)

Returns the value of attribute function.



8
9
10
# File 'lib/mtk/patterns/function.rb', line 8

def function
  @function
end

Instance Method Details

#advanceObject (protected)



26
27
28
29
30
31
32
33
34
# File 'lib/mtk/patterns/function.rb', line 26

def advance
  @function_call_count += 1
  @current = case @function.arity
    when 0 then @function.call
    when 1 then @function.call(@current)
    when 2 then @function.call(@current, @function_call_count)
    else @function.call(@current, @function_call_count, @element_count)
  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



21
22
23
24
# File 'lib/mtk/patterns/function.rb', line 21

def rewind_or_cycle(is_cycling=false)
  @function_call_count = -1
  super
end