Class: MTK::Patterns::Function
- Defined in:
- lib/mtk/patterns/function.rb
Overview
An arbitrary function that dynamically generates elements.
Instance Attribute Summary collapse
-
#function ⇒ Object
readonly
Returns the value of attribute function.
Attributes inherited from Pattern
#cycle_count, #element_count, #elements, #max_cycles, #max_elements, #min_elements, #options
Instance Method Summary collapse
- #advance ⇒ Object protected
-
#initialize(elements, options = {}) ⇒ Function
constructor
A new instance of Function.
-
#rewind_or_cycle(is_cycling = false) ⇒ Object
protected
Reset the pattern to the beginning.
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, ={}) # 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], end |
Instance Attribute Details
#function ⇒ Object (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
#advance ⇒ Object (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
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 |