Module: EventMapper

Defined in:
lib/eventmapper.rb,
lib/eventmapper/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#event_blocks?Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
# File 'lib/eventmapper.rb', line 9

def event_blocks?
  if @event_blocks.nil?
    @event_blocks = true
  else
    @event_blocks
  end
end

#fire(event, *args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/eventmapper.rb', line 27

def fire(event, *args)
  if @event_blocks.nil?
    @event_blocks = true
  end

  if args.empty?
    @events[event].each do |proc|
      if @event_blocks
        proc.call
      else
        Thread.new { proc.call }
      end
    end
  else
    @events[event].each do |proc|
      if @event_blocks
        proc.call *args
      else
        Thread.new { proc.call *args }
      end
    end
  end
end

#on(event, &block) ⇒ Object



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

def on(event, &block)
  if @events.nil?
    @events = { event => [block] }
  elsif @events[event].nil?
    @events[event] = [block]
  else
    @events[event] << block
  end
end

#set_event_blocking(bool) ⇒ Object



5
6
7
# File 'lib/eventmapper.rb', line 5

def set_event_blocking(bool)
  @event_blocks = bool
end