Module: Railsmith::Hooks::Dsl::ClassMethods

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

Overview

Class-level macros installed on every BaseService subclass.

Instance Method Summary collapse

Instance Method Details

#after(*actions, **options) ⇒ Object

Declare an after hook. The block receives the service’s Result as its block argument and runs after the action completes (successful or not). After hooks cannot change the returned Result.



45
46
47
# File 'lib/railsmith/hooks/dsl.rb', line 45

def after(*actions, **options, &)
  register_hook(:after, actions, options, &)
end

#around(*actions, **options) ⇒ Object

Declare an around hook. The block receives a callable action; it must invoke action.call somewhere in its body or an AroundHookNotYieldedError is raised. Whatever the block returns becomes the Result for the call, so around hooks can short-circuit or transform the outcome.



54
55
56
# File 'lib/railsmith/hooks/dsl.rb', line 54

def around(*actions, **options, &)
  register_hook(:around, actions, options, &)
end

#before(*actions, **options) ⇒ Object

Declare a before hook on one or more actions. The block runs just before the action method and is evaluated in the service instance context (so context, params, and service helpers are available).

Parameters:

  • actions (Array<Symbol>)

    one or more action symbols

  • if (Symbol, Proc)

    optional method name or callable guard

  • unless (Symbol, Proc)

    optional inverted guard

  • name (Symbol)

    optional name for skip_before targeting



38
39
40
# File 'lib/railsmith/hooks/dsl.rb', line 38

def before(*actions, **options, &)
  register_hook(:before, actions, options, &)
end

#hook_registryObject

Returns the HookRegistry for this class, creating it on first use.



82
83
84
# File 'lib/railsmith/hooks/dsl.rb', line 82

def hook_registry
  @hook_registry ||= HookRegistry.new
end

#hooks_for(action) ⇒ Object

Introspection helper: returns the effective HookChain for an action on this class, including inherited entries but excluding global hooks (global hooks are configuration, not part of the class itself).



89
90
91
# File 'lib/railsmith/hooks/dsl.rb', line 89

def hooks_for(action)
  hook_registry.chain.for_action(action)
end

#inherited(subclass) ⇒ Object

Propagate the parent registry into subclasses so child declarations append to a private copy instead of mutating the parent’s chain.



95
96
97
98
# File 'lib/railsmith/hooks/dsl.rb', line 95

def inherited(subclass)
  super
  subclass.instance_variable_set(:@hook_registry, hook_registry.dup)
end

#skip_after(*args) ⇒ Object

Remove an inherited after hook by name.



72
73
74
# File 'lib/railsmith/hooks/dsl.rb', line 72

def skip_after(*args)
  hook_registry.remove(name: args.last, type: :after)
end

#skip_around(*args) ⇒ Object

Remove an inherited around hook by name.



77
78
79
# File 'lib/railsmith/hooks/dsl.rb', line 77

def skip_around(*args)
  hook_registry.remove(name: args.last, type: :around)
end

#skip_before(*args) ⇒ Object

Remove an inherited before hook by name. Extra leading arguments (typically action symbols) are accepted for documentation value and ignored — the last symbol is the hook name.



67
68
69
# File 'lib/railsmith/hooks/dsl.rb', line 67

def skip_before(*args)
  hook_registry.remove(name: args.last, type: :before)
end

#skip_hook(name) ⇒ Object

Remove an inherited hook by name, regardless of type. See ADR-0002 for why only named hooks can be skipped.



60
61
62
# File 'lib/railsmith/hooks/dsl.rb', line 60

def skip_hook(name)
  hook_registry.remove(name: name)
end