Class: Sequencer::Event

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

Overview

Events that are fired by the sequencer

Instance Method Summary collapse

Constructor Details

#initializeEvent

Returns a new instance of Event


6
7
8
9
10
11
# File 'lib/sequencer/event.rb', line 6

def initialize
  @next = {}
  @perform = []
  @step = []
  @stop = []
end

Instance Method Details

#do_next(pointer, data) ⇒ Array<Object>

Parameters:

  • pointer (Fixnum, nil)

    The pointer number (or nil) to fire callbacks for.

  • data (Object)

    The data for the current pointer

Returns:

  • (Array<Object>)

36
37
38
39
40
# File 'lib/sequencer/event.rb', line 36

def do_next(pointer, data)
  keys = [pointer, nil]
  callbacks = keys.map { |key| @next.delete(key) }.flatten.compact
  callbacks.map(&:call)
end

#do_perform(data) ⇒ Boolean

Fire the perform event

Parameters:

  • data (Object)

    Data for the current sequence step

Returns:

  • (Boolean)

90
91
92
# File 'lib/sequencer/event.rb', line 90

def do_perform(data)
  @perform.map { |callback| callback.call(data) }
end

#do_stepBoolean

Fire the step events

Returns:

  • (Boolean)

55
56
57
# File 'lib/sequencer/event.rb', line 55

def do_step
  @step.map(&:call)
end

#do_stopBoolean

Fire the stop event

Returns:

  • (Boolean)

72
73
74
# File 'lib/sequencer/event.rb', line 72

def do_stop
  @stop.map(&:call)
end

#next(pointer = nil, &block) ⇒ Array<Proc>

Fire an event the next time the pointer reaches the given number

Parameters:

  • pointer (Fixnum, nil) (defaults to: nil)

    The pointer number when the callback should be fired. If nil, it will be fired whenever Event#do_next is called.

  • block (Proc)

Returns:

  • (Array<Proc>)

17
18
19
20
21
22
23
24
# File 'lib/sequencer/event.rb', line 17

def next(pointer = nil, &block)
  @next[pointer] ||= []
  if block_given?
    @next[pointer].clear
    @next[pointer] << block
  end
  @next[pointer]
end

#next?(pointer = nil) ⇒ Boolean

Whether any callbacks exist for the given pointer (or nil)

Parameters:

  • pointer (Fixnum, nil) (defaults to: nil)

Returns:

  • (Boolean)

29
30
31
# File 'lib/sequencer/event.rb', line 29

def next?(pointer = nil)
  !@next[pointer].nil?
end

#perform(&block) ⇒ Proc

Set the perform event

Parameters:

  • block (Proc)

Returns:

  • (Proc)

79
80
81
82
83
84
85
# File 'lib/sequencer/event.rb', line 79

def perform(&block)
  if block_given?
    @perform.clear
    @perform << block
  end
  @perform
end

#step(&block) ⇒ Proc

Set the step event

Parameters:

  • block (Proc)

Returns:

  • (Proc)

45
46
47
48
49
50
51
# File 'lib/sequencer/event.rb', line 45

def step(&block)
  if block_given?
    @step.clear
    @step << block
  end
  @step
end

#stop(&block) ⇒ Proc

Access the stop events

Parameters:

  • block (Proc)

Returns:

  • (Proc)

62
63
64
65
66
67
68
# File 'lib/sequencer/event.rb', line 62

def stop(&block)
  if block_given?
    @stop.clear
    @stop << block
  end
  @stop
end