Class: CMDx::CallbackRegistry
- Inherits:
-
Object
- Object
- CMDx::CallbackRegistry
- Extended by:
- Forwardable
- 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.
Supports copy-on-write semantics: a duped registry shares the parent’s data until a write operation triggers materialization.
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 Method Summary collapse
-
#deregister(type, *callables, **options, &block) ⇒ CallbackRegistry
Removes one or more callables for a specific callback type.
-
#initialize(registry = nil) ⇒ CallbackRegistry
constructor
A new instance of CallbackRegistry.
-
#initialize_dup(source) ⇒ Object
Sets up copy-on-write state when duplicated via dup.
-
#invoke(type, task) ⇒ Object
Invokes all registered callbacks for a given type.
-
#register(type, *callables, **options, &block) ⇒ CallbackRegistry
Registers one or more callables for a specific callback type.
-
#registry ⇒ Hash{Symbol => Set<Array>}
(also: #to_h)
Returns the internal registry of callbacks organized by type.
Constructor Details
#initialize(registry = nil) ⇒ CallbackRegistry
Returns a new instance of CallbackRegistry.
38 39 40 |
# File 'lib/cmdx/callback_registry.rb', line 38 def initialize(registry = nil) @registry = registry || {} end |
Instance Method Details
#deregister(type, *callables, **options, &block) ⇒ CallbackRegistry
Removes one or more callables for a specific callback type
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/cmdx/callback_registry.rb', line 112 def deregister(type, *callables, **, &block) materialize! callables << block if block_given? return self unless @registry[type] @registry[type].delete([callables, ]) @registry.delete(type) if @registry[type].empty? self end |
#initialize_dup(source) ⇒ Object
Sets up copy-on-write state when duplicated via dup.
47 48 49 50 51 |
# File 'lib/cmdx/callback_registry.rb', line 47 def initialize_dup(source) @parent = source @registry = nil super end |
#invoke(type, task) ⇒ Object
Invokes all registered callbacks for a given type
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/cmdx/callback_registry.rb', line 134 def invoke(type, task) raise TypeError, "unknown callback type #{type.inspect}" unless TYPES_SET.include?(type) return unless registry[type] registry[type].each do |callables, | next unless Utils::Condition.evaluate(task, ) Utils::Wrap.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
88 89 90 91 92 93 94 95 96 |
# File 'lib/cmdx/callback_registry.rb', line 88 def register(type, *callables, **, &block) materialize! callables << block if block_given? @registry[type] ||= Set.new @registry[type] << [callables, ] self end |
#registry ⇒ Hash{Symbol => Set<Array>} Also known as: to_h
Returns the internal registry of callbacks organized by type. Delegates to the parent registry when not yet materialized.
62 63 64 |
# File 'lib/cmdx/callback_registry.rb', line 62 def registry @registry || @parent.registry end |