Class: Gaskit::HookRegistry
- Inherits:
-
Object
- Object
- Gaskit::HookRegistry
- Defined in:
- lib/gaskit/hook_registry.rb
Overview
Gaskit::HookRegistry manages registration and retrieval of global hooks for operation-style lifecycles (e.g., ‘before`, `after`, `around`).
Hooks are grouped by type and tag. They are used in classes that include ‘Gaskit::Hookable`.
Instance Method Summary collapse
-
#fetch(type, tags = nil) ⇒ Array<#call>
Fetches hooks for the given type, filtered by tags.
-
#initialize ⇒ HookRegistry
constructor
A new instance of HookRegistry.
-
#register(type, tag, callable = nil) {|hook| ... } ⇒ void
Registers a new hook under a given type and tag.
-
#registered?(type, tag) ⇒ Boolean
Checks if a hook tag is registered under a specific type.
-
#registered_tags(type) ⇒ Array<Symbol>
Returns all registered tags for a given hook type.
Constructor Details
#initialize ⇒ HookRegistry
Returns a new instance of HookRegistry.
9 10 11 12 13 14 15 |
# File 'lib/gaskit/hook_registry.rb', line 9 def initialize @hooks = { before: Hash.new { |h, k| h[k] = [] }, after: Hash.new { |h, k| h[k] = [] }, around: Hash.new { |h, k| h[k] = [] } } end |
Instance Method Details
#fetch(type, tags = nil) ⇒ Array<#call>
Fetches hooks for the given type, filtered by tags.
48 49 50 51 52 |
# File 'lib/gaskit/hook_registry.rb', line 48 def fetch(type, = nil) return @hooks[type].values.flatten if .nil? || .empty? ( || []).flat_map { |tag| @hooks[type][tag.to_sym] } end |
#register(type, tag, callable = nil) {|hook| ... } ⇒ void
This method returns an undefined value.
Registers a new hook under a given type and tag.
25 26 27 28 29 30 31 |
# File 'lib/gaskit/hook_registry.rb', line 25 def register(type, tag, callable = nil, &block) hook = callable || block raise ArgumentError, "Hook must respond to #call" unless hook.respond_to?(:call) raise ArgumentError, "Unknown hook type: #{type}" unless @hooks.key?(type) @hooks[type][tag.to_sym] << hook end |
#registered?(type, tag) ⇒ Boolean
Checks if a hook tag is registered under a specific type.
38 39 40 |
# File 'lib/gaskit/hook_registry.rb', line 38 def registered?(type, tag) @hooks[type].key?(tag.to_sym) end |
#registered_tags(type) ⇒ Array<Symbol>
Returns all registered tags for a given hook type.
58 59 60 |
# File 'lib/gaskit/hook_registry.rb', line 58 def (type) @hooks[type].keys end |