Class: CMDx::CallbackRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdx/callback_registry.rb

Overview

Registry for managing callbacks that can be executed at various points during task execution.

Callbacks are organized by type and can be registered with optional conditions and options. Each callback type represents a specific execution phase or outcome.

Constant Summary collapse

TYPES =
%i[
  before_validation
  before_execution
  on_complete
  on_interrupted
  on_executed
  on_success
  on_skipped
  on_failed
  on_good
  on_bad
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry = {}) ⇒ CallbackRegistry

Returns a new instance of CallbackRegistry.

Parameters:

  • registry (Hash) (defaults to: {})

    Initial registry hash, defaults to empty



38
39
40
# File 'lib/cmdx/callback_registry.rb', line 38

def initialize(registry = {})
  @registry = registry
end

Instance Attribute Details

#registryHash{Symbol => Set<Array>} (readonly) Also known as: to_h

Returns the internal registry of callbacks organized by type.

Examples:

registry.registry # => { before_execution: #<Set: [[[:validate], {}]]> }

Returns:

  • (Hash{Symbol => Set<Array>})

    Hash mapping callback types to their registered callables



32
33
34
# File 'lib/cmdx/callback_registry.rb', line 32

def registry
  @registry
end

Instance Method Details

#deregister(type, *callables, **options, &block) ⇒ CallbackRegistry

Removes one or more callables for a specific callback type

Examples:

Remove a specific callback

registry.deregister(:before_execution, :validate_inputs)

Parameters:

  • type (Symbol)

    The callback type from TYPES

  • callables (Array<#call>)

    Callable objects to remove

  • options (Hash)

    Options that were used during registration

  • block (Proc)

    Optional block to remove

Options Hash (**options):

  • :* (Object)

    Any option key-value pairs

Returns:



94
95
96
97
98
99
100
101
# File 'lib/cmdx/callback_registry.rb', line 94

def deregister(type, *callables, **options, &block)
  callables << block if block_given?
  return self unless registry[type]

  registry[type].delete([callables, options])
  registry.delete(type) if registry[type].empty?
  self
end

#dupCallbackRegistry

Creates a deep copy of the registry with duplicated callable sets

Returns:



47
48
49
# File 'lib/cmdx/callback_registry.rb', line 47

def dup
  self.class.new(registry.transform_values(&:dup))
end

#invoke(type, task) ⇒ Object

Invokes all registered callbacks for a given type

Examples:

Invoke all before_execution callbacks

registry.invoke(:before_execution, task)

Parameters:

  • type (Symbol)

    The callback type to invoke

  • task (Task)

    The task instance to pass to callbacks

Raises:

  • (TypeError)

    When type is not a valid callback type



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cmdx/callback_registry.rb', line 114

def invoke(type, task)
  raise TypeError, "unknown callback type #{type.inspect}" unless TYPES.include?(type)

  Array(registry[type]).each do |callables, options|
    next unless Utils::Condition.evaluate(task, options)

    Array(callables).each do |callable|
      if callable.is_a?(Symbol)
        task.send(callable)
      elsif callable.is_a?(Proc)
        task.instance_exec(&callable)
      elsif callable.respond_to?(:call)
        callable.call(task)
      else
        raise "cannot invoke #{callable}"
      end
    end
  end
end

#register(type, *callables, **options, &block) ⇒ CallbackRegistry

Registers one or more callables for a specific callback type

Examples:

Register a method callback

registry.register(:before_execution, :validate_inputs)

Register a block with conditions

registry.register(:on_success, if: { status: :completed }) do |task|
  task.log("Success callback executed")
end

Parameters:

  • type (Symbol)

    The callback type from TYPES

  • callables (Array<#call>)

    Callable objects to register

  • options (Hash)

    Options to pass to the callback

  • block (Proc)

    Optional block to register as a callable

Options Hash (**options):

  • :if (Hash)

    Condition hash for conditional execution

  • :unless (Hash)

    Inverse condition hash for conditional execution

Returns:

Raises:

  • (ArgumentError)

    When type is not a valid callback type



72
73
74
75
76
77
78
# File 'lib/cmdx/callback_registry.rb', line 72

def register(type, *callables, **options, &block)
  callables << block if block_given?

  registry[type] ||= Set.new
  registry[type] << [callables, options]
  self
end