Module: Operandi::Callbacks

Included in:
Base
Defined in:
lib/operandi/callbacks.rb

Overview

Provides callback hooks for service and step lifecycle events.

Examples:

Service-level callbacks

class MyService < Operandi::Base
  before_service_run :log_start
  after_service_run { |service| Rails.logger.info("Done!") }
  on_service_success :send_notification
  on_service_failure :log_error
end

Step-level callbacks

class MyService < Operandi::Base
  before_step_run :log_step_start
  after_step_run { |service, step_name| puts "Finished #{step_name}" }
  on_step_failure :handle_step_error
end

Around callbacks

class MyService < Operandi::Base
  around_service_run :with_timing

  private

  def with_timing(service)
    start = Time.now
    yield
    puts "Took #{Time.now - start}s"
  end
end

Constant Summary collapse

EVENTS =

Available callback events.

Returns:

  • (Array<Symbol>)

    list of callback event names

[
  :before_step_run,
  :after_step_run,
  :around_step_run,
  :on_step_success,
  :on_step_failure,
  :on_step_crash,
  :before_service_run,
  :after_service_run,
  :around_service_run,
  :on_service_success,
  :on_service_failure,
].freeze

Instance Method Summary collapse

Instance Method Details

#run_callbacks(event, *args) { ... } ⇒ void

This method returns an undefined value.

Run all callbacks for a given event.

Parameters:

  • event (Symbol)

    the callback event name

  • args (Array)

    arguments to pass to callbacks

Yields:

  • for around callbacks, the block to wrap



56
57
58
59
60
61
62
63
64
65
# File 'lib/operandi/callbacks.rb', line 56

def run_callbacks(event, *args, &)
  callbacks = self.class.all_callbacks_for(event)

  if event.to_s.start_with?("around_")
    run_around_callbacks(callbacks, args, &)
  else
    run_simple_callbacks(callbacks, args)
    yield if block_given?
  end
end