Class: Railsmith::Hooks::HookRegistry
- Inherits:
-
Object
- Object
- Railsmith::Hooks::HookRegistry
- 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
-
#chain ⇒ Object
readonly
The current chain (immutable snapshot).
Instance Method Summary collapse
-
#add(entry) ⇒ Object
Append an entry, returning self for chaining.
- #any? ⇒ Boolean
-
#dup ⇒ Object
Deep-dup: produce a new registry with the same (frozen) chain reference.
- #empty? ⇒ Boolean
- #entries ⇒ Object
-
#initialize(chain: HookChain.new) ⇒ HookRegistry
constructor
A new instance of HookRegistry.
-
#remove(name:, type: nil) ⇒ Object
Remove every entry with the given name (optionally restricted to a type).
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
#chain ⇒ Object (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
44 45 46 |
# File 'lib/railsmith/hooks/hook_registry.rb', line 44 def any? !empty? end |
#dup ⇒ Object
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
40 41 42 |
# File 'lib/railsmith/hooks/hook_registry.rb', line 40 def empty? @chain.empty? end |
#entries ⇒ Object
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 |