Class: Railsmith::Hooks::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/railsmith/hooks/runner.rb

Overview

Executes the hook chain around a service action.

Construction merges the global hook chain (from Railsmith.configuration) with the class-level chain on the service, filters by action and applicability (if:/unless:, only:), then splits into before / around / after phases.

Hook blocks are evaluated with instance_exec on the service instance so the block body has access to params, context, etc. – matching the ergonomics developers expect from ActiveRecord-style callbacks.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance:, action:, global_chain: nil) ⇒ Runner

Returns a new instance of Runner.



16
17
18
19
20
21
# File 'lib/railsmith/hooks/runner.rb', line 16

def initialize(instance:, action:, global_chain: nil)
  @instance = instance
  @action = action.to_sym
  @service_class = instance.class
  @global_chain = global_chain || Runner.global_chain
end

Class Method Details

.global_chainObject

Class-level convenience: the current global HookChain, pulled fresh from configuration so tests can mutate it between calls.



42
43
44
# File 'lib/railsmith/hooks/runner.rb', line 42

def self.global_chain
  Railsmith.configuration.global_hooks.chain
end

Instance Method Details

#run(&action_block) ⇒ Object

Entry point: runs the hook chain, yielding to action_block when the innermost point of the sandwich is reached. Returns whatever the chain ultimately produces – normally the service’s Result.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/railsmith/hooks/runner.rb', line 26

def run(&action_block)
  resolved = resolve_chain
  return action_block.call if resolved.empty?

  befores = resolved.of_type(:before).entries
  arounds = resolved.of_type(:around).entries
  afters  = resolved.of_type(:after).entries

  run_befores(befores)
  result = run_arounds(arounds, &action_block)
  run_afters(afters, result)
  result
end