Class: Vedeu::Events
- Inherits:
-
Object
- Object
- Vedeu::Events
- Defined in:
- lib/vedeu/support/events.rb
Instance Attribute Summary collapse
-
#handlers ⇒ Object
readonly
private
Returns the value of attribute handlers.
Instance Method Summary collapse
-
#add(object, &block) ⇒ Object
[].
- #event(name, opts = {}, &block) ⇒ Object
- #initialize(&block) ⇒ Events constructor
-
#method_missing(method, *args, &block) ⇒ Object
private
private
[].
-
#registered ⇒ Array
Returns a collection of the names of all the registered events.
-
#reset ⇒ Object
Remove all registered events.
- #trigger(name, *args) ⇒ Object
-
#unevent(name) ⇒ Object
Unregisters the event by name, effectively deleting the associated events bound with it also.
Constructor Details
#initialize(&block) ⇒ Events
6 7 8 9 10 |
# File 'lib/vedeu/support/events.rb', line 6 def initialize(&block) @handlers = Hash.new { |hash, key| hash[key] = { events: [] } } instance_eval(&block) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns [].
61 62 63 |
# File 'lib/vedeu/support/events.rb', line 61 def method_missing(method, *args, &block) @self_before_instance_eval.send(method, *args, &block) end |
Instance Attribute Details
#handlers ⇒ Object (readonly, private)
Returns the value of attribute handlers.
57 58 59 |
# File 'lib/vedeu/support/events.rb', line 57 def handlers @handlers end |
Instance Method Details
#add(object, &block) ⇒ Object
Returns [].
15 16 17 18 19 |
# File 'lib/vedeu/support/events.rb', line 15 def add(object, &block) @self_before_instance_eval = eval('self', block.binding) instance_eval(&block) end |
#event(name, opts = {}, &block) ⇒ Object
22 23 24 25 |
# File 'lib/vedeu/support/events.rb', line 22 def event(name, opts = {}, &block) handlers[name][:events] << Event.new(block, opts) handlers[name] end |
#registered ⇒ Array
Returns a collection of the names of all the registered events.
39 40 41 |
# File 'lib/vedeu/support/events.rb', line 39 def registered handlers.keys end |
#reset ⇒ Object
Remove all registered events. Used for testing purposes.
51 52 53 |
# File 'lib/vedeu/support/events.rb', line 51 def reset @handlers = Hash.new { |hash, key| hash[key] = { events: [] } } end |
#trigger(name, *args) ⇒ Object
44 45 46 |
# File 'lib/vedeu/support/events.rb', line 44 def trigger(name, *args) handlers[name][:events].each { |event| event.trigger(*args) } end |
#unevent(name) ⇒ Object
Unregisters the event by name, effectively deleting the associated events bound with it also.
32 33 34 |
# File 'lib/vedeu/support/events.rb', line 32 def unevent(name) handlers.delete_if { |k, v| k == name } end |