Class: Sequencer
- Inherits:
-
Object
- Object
- Sequencer
- Defined in:
- lib/sequencer.rb
Overview
A class for running arbitrary actions in sequence.
Instance Attribute Summary collapse
-
#running ⇒ Object
readonly
Returns the value of attribute running.
Instance Method Summary collapse
-
#add(f) ⇒ Object
Add a function to the sequence.
-
#initialize(procs = []) ⇒ Sequencer
constructor
A new instance of Sequencer.
-
#next ⇒ Object
Run the next function (if any).
-
#reverse! ⇒ Object
Reverse the order of the sequence.
-
#start ⇒ Object
Start running.
-
#start_callback ⇒ Object
Return a proc to trigger the start.
Constructor Details
#initialize(procs = []) ⇒ Sequencer
Returns a new instance of Sequencer.
6 7 8 9 |
# File 'lib/sequencer.rb', line 6 def initialize(procs=[]) @procs = procs @running = false end |
Instance Attribute Details
#running ⇒ Object (readonly)
Returns the value of attribute running.
4 5 6 |
# File 'lib/sequencer.rb', line 4 def running @running end |
Instance Method Details
#add(f) ⇒ Object
Add a function to the sequence
12 13 14 |
# File 'lib/sequencer.rb', line 12 def add(f) @procs.push(f) end |
#next ⇒ Object
Run the next function (if any)
35 36 37 38 39 40 41 |
# File 'lib/sequencer.rb', line 35 def next if @procs.size > 0 @procs.shift.call(proc { self.next }) else @running = false end end |
#reverse! ⇒ Object
Reverse the order of the sequence
17 18 19 |
# File 'lib/sequencer.rb', line 17 def reverse! @procs.reverse! end |
#start ⇒ Object
Start running
22 23 24 25 |
# File 'lib/sequencer.rb', line 22 def start @running = true self.next end |
#start_callback ⇒ Object
Return a proc to trigger the start
28 29 30 31 32 |
# File 'lib/sequencer.rb', line 28 def start_callback proc { self.start } end |