Class: Ray::DSL::EventRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/ray/dsl/event_runner.rb

Overview

This class is the one that dispatches events in your program, the one that makes everything work. You may want to create one by yourself if you don’t want to use the other classes that use it. You just have to call run every time you need the events to be processed.

Instance Method Summary collapse

Constructor Details

#initializeEventRunner

Returns a new instance of EventRunner.



8
9
10
11
12
13
14
# File 'lib/ray/dsl/event_runner.rb', line 8

def initialize
  @handlers    = []
  @next_events = []

  @event_groups = Hash.new { |h, k| h[k] = true }
  @event_groups[:default] = true
end

Instance Method Details

#add_event(type, args) ⇒ Object



32
33
34
# File 'lib/ray/dsl/event_runner.rb', line 32

def add_event(type, args)
  @next_events << Ray::DSL::Event.new(type, args)
end

#add_handler(type, group, args, block) ⇒ Object



28
29
30
# File 'lib/ray/dsl/event_runner.rb', line 28

def add_handler(type, group, args, block)
  @handlers << Ray::DSL::Handler.new(type, group, args, block)
end

#clearObject

Removes all the registered handlers



52
53
54
# File 'lib/ray/dsl/event_runner.rb', line 52

def clear
  @handlers.clear
end

#disable_group(group) ⇒ Object

Disables an event group



37
38
39
# File 'lib/ray/dsl/event_runner.rb', line 37

def disable_group(group)
  @event_groups[group] = false
end

#enable_group(group) ⇒ Object

Enables an event group



42
43
44
# File 'lib/ray/dsl/event_runner.rb', line 42

def enable_group(group)
  @event_groups[group] = true
end

#group_enabled?(group) ⇒ Boolean

Returns Whether an event group is enabled.

Returns:

  • (Boolean)

    Whether an event group is enabled



57
58
59
# File 'lib/ray/dsl/event_runner.rb', line 57

def group_enabled?(group)
  @event_groups[group]
end

#remove_group(name) ⇒ Object

Removes all the handlers belonging to a given group



47
48
49
# File 'lib/ray/dsl/event_runner.rb', line 47

def remove_group(name)
  @handlers.delete_if { |o| o.group == name }
end

#runObject

Sends all the known events to our listeners.



17
18
19
20
21
22
23
24
25
26
# File 'lib/ray/dsl/event_runner.rb', line 17

def run
  event_list   = @next_events
  @next_events = []

  handlers = @handlers.select { |o| group_enabled?(o.group) }

  event_list.each do |ev|
    handlers.each { |o| o.call(ev) if o.match?(ev) }
  end
end