Module: Railsmith::Hooks::Dsl

Included in:
BaseService
Defined in:
lib/railsmith/hooks/dsl.rb

Overview

Class-level DSL for declaring hooks on a BaseService subclass.

class OrderService < Railsmith::BaseService
  before :create do
    AuditLog.record(action: :create, actor: context[:actor_id])
  end

  after :create do |result|
    EventBus.publish("order.created", result.value) if result.success?
  end

  around :create do |action|
    Metrics.time("order.create") { action.call }
  end
end

Subclasses inherit all hooks from their parent (see ADR-0002). Named hooks can be skipped via skip_before/skip_after/skip_around/skip_hook.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



24
25
26
# File 'lib/railsmith/hooks/dsl.rb', line 24

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

Instance Method Details

#run_lifecycle_hooks(action) ⇒ Object

Instance-level entry point invoked by BaseService#execute_action. Wraps the action dispatch in the full hook sandwich. When no hooks are declared on the class or globally, this is effectively a no-op cost — the Runner short-circuits on an empty chain.



144
145
146
# File 'lib/railsmith/hooks/dsl.rb', line 144

def run_lifecycle_hooks(action, &)
  Runner.new(instance: self, action: action).run(&)
end