Class: Gaskit::HookRegistry

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeHookRegistry

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.

Parameters:

  • type (Symbol)

    The hook type (‘:before`, `:after`, or `:around`)

  • tags (Array<Symbol, String>, nil) (defaults to: nil)

    One or more tags to filter hooks by. If nil or empty, returns all hooks of that type.

Returns:

  • (Array<#call>)

    An array of callable hooks



48
49
50
51
52
# File 'lib/gaskit/hook_registry.rb', line 48

def fetch(type, tags = nil)
  return @hooks[type].values.flatten if tags.nil? || tags.empty?

  (tags || []).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.

Parameters:

  • type (Symbol)

    The lifecycle type: ‘:before`, `:after`, or `:around`

  • tag (Symbol, String)

    A symbolic tag to group related hooks (e.g., :audit, :metrics)

  • callable (#call, nil) (defaults to: nil)

    A callable object (optional if block is given)

Yields:

  • (hook)

    A block to be registered as a hook if no callable is passed

Raises:

  • (ArgumentError)

    If the hook is not callable or the type is invalid



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.

Parameters:

  • type (Symbol)

    The hook type (‘:before`, `:after`, or `:around`)

  • tag (Symbol, String)

    The tag to check

Returns:

  • (Boolean)

    Whether a hook with that tag exists for the given 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.

Parameters:

  • type (Symbol)

    The hook type (‘:before`, `:after`, or `:around`)

Returns:

  • (Array<Symbol>)

    List of registered tags under that type



58
59
60
# File 'lib/gaskit/hook_registry.rb', line 58

def registered_tags(type)
  @hooks[type].keys
end