Module: Easyop::Plugins::Events

Defined in:
lib/easyop/plugins/events.rb

Overview

Emits domain events after an operation completes.

Install on a base operation class to inherit into all subclasses:

class ApplicationOperation
  include Easyop::Operation
  plugin Easyop::Plugins::Events
end

Then declare events on individual operations:

class PlaceOrder < ApplicationOperation
  emits "order.placed", on: :success, payload: [:order_id, :total]
  emits "order.failed", on: :failure, payload: ->(ctx) { { error: ctx.error } }
  emits "order.attempted", on: :always

  def call
    ctx.order_id = Order.create!(ctx.to_h).id
  end
end

Plugin options:

bus:      [Bus::Base, nil]  per-class bus override (default: global registry bus)
metadata: [Hash, Proc, nil] extra metadata merged into every event from this class

‘emits` DSL options:

on:      [:success (default), :failure, :always]
payload: [Proc, Array, nil]  Proc receives ctx; Array slices ctx keys; nil = full ctx.to_h
guard:   [Proc, nil]         extra condition — event only fires if truthy

Defined Under Namespace

Modules: ClassMethods, RunWrapper

Class Method Summary collapse

Class Method Details

.install(base, bus: nil, metadata: nil, **_options) ⇒ Object



35
36
37
38
39
40
# File 'lib/easyop/plugins/events.rb', line 35

def self.install(base, bus: nil, metadata: nil, **_options)
  base.extend(ClassMethods)
  base.prepend(RunWrapper)
  base.instance_variable_set(:@_events_bus,      bus)
  base.instance_variable_set(:@_events_metadata, )
end