Class: Sequencer

Inherits:
Object
  • Object
show all
Defined in:
lib/sequencer.rb

Overview

A class for running arbitrary actions in sequence.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#runningObject (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

#nextObject

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

#startObject

Start running



22
23
24
25
# File 'lib/sequencer.rb', line 22

def start
  @running = true
  self.next
end

#start_callbackObject

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