Module: Easyop::Plugins::Events::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#_emitted_eventsObject



58
59
60
# File 'lib/easyop/plugins/events.rb', line 58

def _emitted_events
  @_emitted_events ||= _inherited_emitted_events
end

#_events_busObject



63
64
65
66
67
68
69
# File 'lib/easyop/plugins/events.rb', line 63

def _events_bus
  if instance_variable_defined?(:@_events_bus)
    @_events_bus
  elsif superclass.respond_to?(:_events_bus)
    superclass._events_bus
  end
end

#_events_metadataObject



72
73
74
75
76
77
78
# File 'lib/easyop/plugins/events.rb', line 72

def 
  if instance_variable_defined?(:@_events_metadata)
    @_events_metadata
  elsif superclass.respond_to?(:_events_metadata)
    superclass.
  end
end

#emits(name, on: :success, payload: nil, guard: nil) ⇒ Object

Declare an event this operation emits after execution.

Examples:

emits "order.placed", on: :success, payload: [:order_id]
emits "order.failed", on: :failure, payload: ->(ctx) { { error: ctx.error } }

Parameters:

  • name (String)
  • on (Symbol) (defaults to: :success)

    :success (default), :failure, or :always

  • payload (Proc, Array, nil) (defaults to: nil)
  • guard (Proc, nil) (defaults to: nil)

    optional condition — fires only when truthy



53
54
55
# File 'lib/easyop/plugins/events.rb', line 53

def emits(name, on: :success, payload: nil, guard: nil)
  _emitted_events << { name: name.to_s, on: on, payload: payload, guard: guard }
end