Class: Vedeu::Events

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/support/events.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Events

Returns a new instance of Events.



3
4
5
6
7
# File 'lib/vedeu/support/events.rb', line 3

def initialize(&block)
  @handlers = Hash.new { |h, k| h[k] = [] }
  self.instance_eval(&block) if block_given?
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

:nocov:



26
27
28
# File 'lib/vedeu/support/events.rb', line 26

def method_missing(method, *args, &block)
  @self_before_instance_eval.send method, *args, &block
end

Instance Method Details

#add(object, &block) ⇒ Object



9
10
11
12
13
# File 'lib/vedeu/support/events.rb', line 9

def add(object, &block)
  @self_before_instance_eval = eval 'self', block.binding

  self.instance_eval(&block)
end

#on(event, &block) ⇒ Object



15
16
17
# File 'lib/vedeu/support/events.rb', line 15

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

#trigger(event, *args) ⇒ Object



19
20
21
22
23
# File 'lib/vedeu/support/events.rb', line 19

def trigger(event, *args)
  handlers[event].each do |handler|
    handler.call(*args)
  end
end