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 |
# 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 |