Module: ActiveFunction::Functions::Callbacks

Defined in:
lib/active_function/functions/callbacks.rb

Overview

Setups #before_action and #after_action callbacks around SuperBase#process using ActiveFunctionCore::Plugins::Hooks. Also provides #define_hooks_for and #set_callback_options for defining custom hooks & options.

defining custom hooks & options.

Examples:

ActiveFunction.plugin :callbacks

class MessagingApp < ActiveFunction::Base
  set_callback_options retries: ->(times, context:) { context.retry if context.retries < times }
  define_hooks_for :retry

  after_action :retry, if: :failed?, only: %i[send_message], retries: 3
  after_retry :increment_retries

  def send_message
    @response.status = 200 if SomeApi.send(@request[:message_content]).success?
  end

  def retry
    @response.committed = false
    process
  end

  private def increment_retries = @response.body[:tries] += 1
  private def failed? = @response.status != 200
  private def retries = @response.body[:tries] ||= 0
end

MessagingApp.process(:send_message, { sender_name: "Alice", message_content: "How are you?" })

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Setup callbacks around Base#process method using ActiveFunctionCore::Plugins::Hooks. Also provides :only option for filtering callbacks by action name.



40
41
42
43
44
# File 'lib/active_function/functions/callbacks.rb', line 40

def self.included(base)
  base.include ActiveFunctionCore::Plugins::Hooks
  base.define_hooks_for :process, name: :action
  base.set_callback_options only: ->(args, context:) { args.to_set === context.action_name }
end

Instance Method Details

#after_action(target, options) ⇒ Object

Parameters:

  • target (Symbol, String)
    • method name to call

Options Hash (options):

  • :if (Symbol, String)
    • method name to check before executing the callback.

  • :unless (Symbol, String)
    • method name to check before executing the callback.

  • :only (Array<Symbol, String>)
    • array of action names.

See Also:

  • ActiveFunctionCore::Plugins::Hooks::ClassMethods#set_callback


# File 'lib/active_function/functions/callbacks.rb', line 46

#before_action(target, options) ⇒ Object

Parameters:

  • target (Symbol, String)
    • method name to call

Options Hash (options):

  • :if (Symbol, String)
    • method name to check before executing the callback.

  • :unless (Symbol, String)
    • method name to check before executing the callback.

  • :only (Array<Symbol, String>)
    • array of action names.

See Also:

  • ActiveFunctionCore::Plugins::Hooks::ClassMethods#set_callback


# File 'lib/active_function/functions/callbacks.rb', line 46

#define_hooks_for(method_name, name: method_name) ⇒ Object

See Also:

  • ActiveFunctionCore::Plugins::Hooks::ClassMethods#define_hooks_for


# File 'lib/active_function/functions/callbacks.rb', line 62

#set_callback(type, hook_name, target, options) ⇒ Object

See Also:

  • ActiveFunctionCore::Plugins::Hooks::ClassMethods#set_callback


# File 'lib/active_function/functions/callbacks.rb', line 59

#set_callback_options(options) ⇒ Object

See Also:

  • ActiveFunctionCore::Plugins::Hooks::ClassMethods#set_callback_options


# File 'lib/active_function/functions/callbacks.rb', line 65