Module: Hifsm::Callbacks

Included in:
Event, State
Defined in:
lib/hifsm/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#set_callbacks(key, listeners) ⇒ Object



4
5
6
7
# File 'lib/hifsm/callbacks.rb', line 4

def set_callbacks(key, listeners)
  @__callbacks ||= {}
  @__callbacks[key] = listeners
end

#trigger(key, target, *args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hifsm/callbacks.rb', line 9

def trigger(key, target, *args)
  return [] unless @__callbacks[key]
  @__callbacks[key].map do |cb|
    if cb.nil?
      # raise something maybe? :)
    elsif cb.is_a? Symbol
      if target.method(cb).arity.zero?
        target.send(cb)
      else
        target.send(cb, *args)
      end
    else
      target.instance_exec(*args, &cb)
    end
  end
end

#trigger?(key, target, *args) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/hifsm/callbacks.rb', line 26

def trigger?(key, target, *args)
  !trigger(key, target, *args).any? {|v| v == false }
end