Module: Ray::DSL::EventListener

Included in:
Helper
Defined in:
lib/ray/dsl/event_listener.rb

Overview

The module that allows you to do something when something else happened.

Instance Method Summary collapse

Instance Method Details

#add_hook(event, ..., callable) ⇒ Object

Same as #on, but the last argument is an object which responds to #to_proc.

Examples:

add_hook :quit, method(:exit!)

Raises:



24
25
26
27
28
# File 'lib/ray/dsl/event_listener.rb', line 24

def add_hook(event, *args)
  raise NoRunnerError, self unless listener_runner
  listener_runner.add_handler(event, current_event_group, args[0...-1],
                              args.last)
end

#current_event_groupObject



45
46
47
# File 'lib/ray/dsl/event_listener.rb', line 45

def current_event_group
  @__listener_event_group ||= :default
end

#current_event_group=(val) ⇒ Object



49
50
51
# File 'lib/ray/dsl/event_listener.rb', line 49

def current_event_group=(val)
  @__listener_event_group = val
end

#event_group(name) { ... } ⇒ Object

Sets the current event group temporarily.

Parameters:

  • name

    Name of the event group.

Yields:

  • Runs a block where registered event will be registred to the event group.



34
35
36
37
38
39
40
41
42
43
# File 'lib/ray/dsl/event_listener.rb', line 34

def event_group(name)
  old_group = current_event_group
  self.current_event_group = name

  begin
    yield name
  ensure
    self.current_event_group = old_group
  end
end

#listener_runnerRay::DSL::EventRunner

Returns Object where matchers will be registred.

Returns:



54
55
56
# File 'lib/ray/dsl/event_listener.rb', line 54

def listener_runner
  @__listener_runner if defined?(@__listener_runner)
end

#listener_runner=(arg) ⇒ Object

Sets the event runner which will be used for listening.

Parameters:



60
61
62
# File 'lib/ray/dsl/event_listener.rb', line 60

def listener_runner=(arg)
  @__listener_runner = arg
end

#on(event, *args, &block) ⇒ Object

Registers a block to listen to an event.

Arguments are compared with the ones of the regex using === and, if it failed, ==.

Parameters:

  • event (Symbol)

    Name of the event to listen to.

  • args (DSL::Matcher, Regexp, Object)

    List of arguments that should match the event’s (if empty, the block will be called anyway).

Raises:



13
14
15
16
# File 'lib/ray/dsl/event_listener.rb', line 13

def on(event, *args, &block)
  raise NoRunnerError, self unless listener_runner
  listener_runner.add_handler(event, current_event_group, args, block)
end