Module: Easyop::Hooks

Defined in:
lib/easyop/hooks.rb

Overview

Lightweight before/after/around hook system with no ActiveSupport dependency.

Hook lists are inherited — a subclass starts with a copy of its parent’s hooks and may add its own without affecting the parent.

Usage:

before :method_name
before { ctx.email = ctx.email.downcase }
after  :send_notification
around :with_logging
around { |inner| Sentry.with_scope { inner.call } }

Execution order:

around hooks wrap everything (outermost first).
Inside the around chain: before hooks → call → after hooks.
after hooks run in an `ensure` block so they always execute.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



19
20
21
# File 'lib/easyop/hooks.rb', line 19

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

Instance Method Details

#with_hooks(&block) ⇒ Object

Run the full hook chain around the user’s ‘call` method. around hooks wrap before+call+after; after hooks always run (ensure).



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/easyop/hooks.rb', line 67

def with_hooks(&block)
  inner = proc do
    run_hooks(self.class._before_hooks)
    begin
      block.call
    ensure
      run_hooks(self.class._after_hooks)
    end
  end

  call_through_around(self.class._around_hooks, inner)
end