Module: Servus::Events::Emitter

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/servus/events/emitter.rb

Overview

Provides event emission DSL for service objects.

This module adds the emits class method to services, allowing them to declare events that will be automatically emitted on success, failure, or error.

Examples:

Basic usage

class CreateUser < Servus::Base
  emits :user_created, on: :success
  emits :user_failed, on: :failure
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.emit_result_events!(instance, result) ⇒ void

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.

This method returns an undefined value.

Emits events for a service result.

Called automatically after service execution completes. Determines the trigger type based on the result and emits all configured events.



27
28
29
30
# File 'lib/servus/events/emitter.rb', line 27

def self.emit_result_events!(instance, result)
  trigger = result.success? ? :success : :failure
  instance.send(:emit_events_for, trigger, result)
end

Instance Method Details

#emit_events_for(trigger, result) ⇒ void

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.

This method returns an undefined value.

Emits events for a specific trigger with the given result.



127
128
129
130
131
132
# File 'lib/servus/events/emitter.rb', line 127

def emit_events_for(trigger, result)
  self.class.emissions_for(trigger).each do |emission|
    payload = build_event_payload(emission, result)
    Servus::Events::Bus.emit(emission[:event_name], payload)
  end
end