Module: ActiveFunctionCore::Plugins::Hooks

Defined in:
lib/active_function_core/plugins/hooks.rb

Overview

Provides ActiveSupport::Callbacks like DSL for defining before and after callbacks around methods

Examples:

Hooks with default callback options

class YourClass
  include ActiveFunctionCore::Plugins::Hooks

  define_hooks_for :your_method

  before_your_method :do_something_before, if: :condition_met?
  after_your_method :do_something_after, unless: :condition_met?

  def your_method
    # Method implementation here...
  end

  private

  def condition_met?
    # Condition logic here...
  end

  def do_something_before
    # Callback logic to execute before your_method
  end

  def do_something_after
    # Callback logic to execute after your_method
  end
end

Hooks with custom callback options

class YourClass
  include ActiveFunction::Core::Plugins::Hooks

  set_callback_options only: ->(only_methods, context:) { only_methods.include?(context.action) }

  define_hooks_for :your_method

  before_your_method :do_something_before, only: %[foo bar]

  def action = "foo"
end

See Also:

Defined Under Namespace

Modules: ClassMethods Classes: Hook

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



210
211
212
# File 'lib/active_function_core/plugins/hooks.rb', line 210

def self.included(base)
  base.extend(ClassMethods)
end