Module: Emittance::Emitter::ClassAndInstanceMethods
- Defined in:
- lib/emittance/emitter.rb
Overview
Included and extended whenever Emittance::Emitter is extended.
Instance Method Summary collapse
-
#emit(identifier, payload: nil) ⇒ Object
Emits an event object to watchers.
-
#emit_with_dynamic_identifier(*identifiers, payload:) ⇒ Object
If you don’t know the specific identifier whose event you want to emit, you can send it a bunch of stuff and
Emitterwill automatically generate anEventclass for you. -
#emits_on(*method_names, identifier: nil) ⇒ Object
Tells the object to emit an event when a any of the given set of methods.
Instance Method Details
#emit(identifier, payload: nil) ⇒ Object
Emits an event object to watchers.
65 66 67 68 69 70 71 72 |
# File 'lib/emittance/emitter.rb', line 65 def emit(identifier, payload: nil) now = Time.now event_klass = _event_klass_for identifier event = event_klass.new(self, now, payload) _send_to_broker event payload end |
#emit_with_dynamic_identifier(*identifiers, payload:) ⇒ Object
If you don’t know the specific identifier whose event you want to emit, you can send it a bunch of stuff and Emitter will automatically generate an Event class for you.
80 81 82 83 84 85 86 87 |
# File 'lib/emittance/emitter.rb', line 80 def emit_with_dynamic_identifier(*identifiers, payload:) now = Time.now event_klass = _event_klass_for(*identifiers) event = event_klass.new(self, now, payload) _send_to_broker event payload end |
#emits_on(*method_names, identifier: nil) ⇒ Object
Tells the object to emit an event when a any of the given set of methods. By default, the event classes are named accordingly: If a Foo object emits_on :bar, then the event’s class will be named FooBarEvent, and will be a subclass of Emittance::Event.
The payload for this event will be the value returned from the method call.
96 97 98 99 100 101 102 |
# File 'lib/emittance/emitter.rb', line 96 def emits_on(*method_names, identifier: nil) method_names.each do |method_name| non_emitting_method = Emittance::Emitter.non_emitting_method_for method_name Emittance::Emitter.emitter_eval(self, &_method_patch_block(method_name, non_emitting_method, identifier)) end end |