Module: EventMapper

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

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#event_blocksObject

Returns the value of attribute event_blocks.



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

def event_blocks
  @event_blocks
end

Instance Method Details

#eventsObject



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

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

#events_sync=(bool) ⇒ Object



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

def events_sync=(bool)
  @events_sync = bool
end

#events_sync?Boolean

Returns:

  • (Boolean)


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

def events_sync?
  @events_sync ||= true
end

#fire(event, *args) ⇒ Object



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

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



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

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