Module: EventMapper

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

Constant Summary collapse

VERSION =
"1.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#event_blocksObject

Returns the value of attribute event_blocks.



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

def event_blocks
  @event_blocks
end

Instance Method Details

#eventsObject



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

def events
  @events ||= Hash.new { |h, k| h[k] = [] }
end

#events_sync=(bool) ⇒ Object



14
15
16
# File 'lib/eventmapper.rb', line 14

def events_sync=(bool)
  @events_sync = bool
end

#events_sync?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/eventmapper.rb', line 10

def events_sync?
  @events_sync ||= true
end

#fire(event, *args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/eventmapper.rb', line 22

def fire(event, *args)
  if args.empty?
    events[event].each do |proc|
      if events_sync?
        proc.call
      else
        Thread.new { proc.call }
      end
    end
  else
    events[event].each do |proc|
      if events_sync?
        proc.call *args
      else
        Thread.new { proc.call *args }
      end
    end
  end
end

#on(event, &block) ⇒ Object



18
19
20
# File 'lib/eventmapper.rb', line 18

def on(event, &block)
  events[event] << block
end