Class: Vedeu::Events
- Inherits:
-
Object
- Object
- Vedeu::Events
- Defined in:
- lib/vedeu/support/events.rb
Instance Method Summary collapse
- #add(object, &block) ⇒ Object
-
#initialize(&block) ⇒ Events
constructor
A new instance of Events.
-
#method_missing(method, *args, &block) ⇒ Object
:nocov:.
- #on(event, &block) ⇒ Object
- #trigger(event, *args) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Events
Returns a new instance of Events.
3 4 5 6 7 8 9 10 11 |
# File 'lib/vedeu/support/events.rb', line 3 def initialize(&block) @handlers = Hash.new { |h, k| h[k] = [] } log("self: #{self.object_id}") 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:
41 42 43 |
# File 'lib/vedeu/support/events.rb', line 41 def method_missing(method, *args, &block) @self_before_instance_eval.send method, *args, &block end |
Instance Method Details
#add(object, &block) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/vedeu/support/events.rb', line 13 def add(object, &block) log("self: #{self.object_id}") log("object: #{object.object_id}") @self_before_instance_eval = eval 'self', block.binding self.instance_eval(&block) end |
#on(event, &block) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/vedeu/support/events.rb', line 22 def on(event, &block) log("self: #{self.object_id}") log("block: #{block.object_id}") log("storing: #{event.inspect}") handlers[event] << block end |
#trigger(event, *args) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/vedeu/support/events.rb', line 30 def trigger(event, *args) handlers[event].each do |handler| log("self: #{self.object_id}") log("handler: #{handler.object_id}") log("running: #{event.inspect}") handler.call(*args) end end |