module Emittance::Emitter::ClassAndInstanceMethods
Included and extended whenever {Emittance::Emitter} is extended.
Public Instance Methods
Emits an {Emittance::Event event object} to watchers.
@param identifier [Symbol, Emittance::Event] either an explicit Event
object or the identifier that can be
parsed into an Event object.
@param payload [*] any additional information that might be helpful for an event's handler to have. Can be
standardized on a per-event basis by pre-defining the class associated with the identifier and validating the payload. See {Emittance::Event} for more details.
@param broker [Symbol] the identifier for the broker you wish to handle the event. Requires additional gems
if not using the default.
@return the payload
# 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
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.
@param identifiers [*] anything that can be used to generate an Event
class. @param payload (@see emit
) @param broker (@see emit
)
# 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
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.
@param method_names [Symbol, String, Array<Symbol, String>] the methods whose calls emit an event
# 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
Private Instance Methods
# File lib/emittance/emitter.rb, line 130 def _event_klass_for(*identifiers) Emittance::Event.event_klass_for(*identifiers) end
# File lib/emittance/emitter.rb, line 106 def _method_patch_block(method_name, non_emitting_method, identifier) lambda do |_klass| return if method_defined?(non_emitting_method) alias_method non_emitting_method, method_name module_eval _method_patch_str(method_name, non_emitting_method, identifier), __FILE__, __LINE__ + 1 end end
# File lib/emittance/emitter.rb, line 116 def _method_patch_str(method_name, non_emitting_method, identifier) <<-RUBY def #{method_name}(*args, &blk) return_value = #{non_emitting_method}(*args, &blk) if #{!identifier.nil? ? true : false} emit #{!identifier.nil? ? identifier : false}, payload: return_value else emit_with_dynamic_identifier self.class, __method__, payload: return_value end return_value end RUBY end
# File lib/emittance/emitter.rb, line 134 def _send_to_broker(event) Emittance::Brokerage.send_event event end