Class: Railsmith::Hooks::HookRegistry

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

Overview

Per-class (or global) store of declared hooks.

The registry is the only mutable container in the hook system — the internal HookChain is replaced wholesale on every change, preserving immutability of previously-published chains. This matches the pattern used by InputRegistry and AssociationRegistry.

On class inheritance, BaseService calls #dup on the parent registry to give the subclass its own copy, so declarations on the subclass do not leak back upward. (See ADR-0002 for inheritance rules.)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chain: HookChain.new) ⇒ HookRegistry

Returns a new instance of HookRegistry.



16
17
18
# File 'lib/railsmith/hooks/hook_registry.rb', line 16

def initialize(chain: HookChain.new)
  @chain = chain
end

Instance Attribute Details

#chainObject (readonly)

The current chain (immutable snapshot).



34
35
36
# File 'lib/railsmith/hooks/hook_registry.rb', line 34

def chain
  @chain
end

Instance Method Details

#add(entry) ⇒ Object

Append an entry, returning self for chaining.



21
22
23
24
25
# File 'lib/railsmith/hooks/hook_registry.rb', line 21

def add(entry)
  next_chain = @chain.append(entry)
  @chain = next_chain
  self
end

#any?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/railsmith/hooks/hook_registry.rb', line 44

def any?
  !empty?
end

#dupObject

Deep-dup: produce a new registry with the same (frozen) chain reference. Chains are immutable, so sharing the frozen chain is safe; only the registry wrapper needs its own identity for independent future growth.



51
52
53
# File 'lib/railsmith/hooks/hook_registry.rb', line 51

def dup
  self.class.new(chain: @chain)
end

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/railsmith/hooks/hook_registry.rb', line 40

def empty?
  @chain.empty?
end

#entriesObject



36
37
38
# File 'lib/railsmith/hooks/hook_registry.rb', line 36

def entries
  @chain.entries
end

#remove(name:, type: nil) ⇒ Object

Remove every entry with the given name (optionally restricted to a type).



28
29
30
31
# File 'lib/railsmith/hooks/hook_registry.rb', line 28

def remove(name:, type: nil)
  @chain = @chain.without(name, type: type)
  self
end