Module: Karafka::Web::Ui::Controllers::Requests::Hookable

Included in:
BaseController
Defined in:
lib/karafka/web/ui/controllers/requests/hookable.rb

Overview

Adds before/after hook support for controller methods.

This module allows registering callbacks that run before and after any named method (e.g., call, show) or for all methods.

Hooks are inherited from parent controllers, making it easy to define shared behavior across a hierarchy.

Examples:

Adding hooks to a controller

class MyController
  include Hookable

  before(:call) { puts "before call" }
  after(:call)  { puts "after call" }

  def call
    run_before_hooks(:call)
    # actual logic
    run_after_hooks(:call)
  end
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Hook into class inclusion to extend DSL

Parameters:

  • base (Class)

    the class including this module



33
34
35
# File 'lib/karafka/web/ui/controllers/requests/hookable.rb', line 33

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

Instance Method Details

#run_after_hooks(action_name) ⇒ Object

Run all after hooks matching the action

Parameters:

  • action_name (Symbol)

    the method name being invoked



89
90
91
92
93
# File 'lib/karafka/web/ui/controllers/requests/hookable.rb', line 89

def run_after_hooks(action_name)
  self.class.after_hooks.each do |actions, block|
    instance_exec(&block) if actions.empty? || actions.include?(action_name)
  end
end

#run_before_hooks(action_name) ⇒ Object

Run all before hooks matching the action

Parameters:

  • action_name (Symbol)

    the method name being invoked



80
81
82
83
84
# File 'lib/karafka/web/ui/controllers/requests/hookable.rb', line 80

def run_before_hooks(action_name)
  self.class.before_hooks.each do |actions, block|
    instance_exec(&block) if actions.empty? || actions.include?(action_name)
  end
end