Class: CMDx::CallbackRegistry
- Inherits:
-
Object
- Object
- CMDx::CallbackRegistry
- 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
-
#registry ⇒ Hash{Symbol => Set<Array>}
(also: #to_h)
readonly
Returns the internal registry of callbacks organized by type.
Instance Method Summary collapse
-
#deregister(type, *callables, **options, &block) ⇒ CallbackRegistry
Removes one or more callables for a specific callback type.
-
#dup ⇒ CallbackRegistry
Creates a deep copy of the registry with duplicated callable sets.
-
#initialize(registry = {}) ⇒ CallbackRegistry
constructor
A new instance of CallbackRegistry.
-
#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.
Constructor Details
#initialize(registry = {}) ⇒ CallbackRegistry
Returns a new instance of CallbackRegistry.
38 39 40 |
# File 'lib/cmdx/callback_registry.rb', line 38 def initialize(registry = {}) @registry = registry end |
Instance Attribute Details
#registry ⇒ Hash{Symbol => Set<Array>} (readonly) Also known as: to_h
Returns the internal registry of callbacks organized by type.
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
94 95 96 97 98 99 100 101 |
# File 'lib/cmdx/callback_registry.rb', line 94 def deregister(type, *callables, **, &block) callables << block if block_given? return self unless registry[type] registry[type].delete([callables, ]) registry.delete(type) if registry[type].empty? self end |
#dup ⇒ CallbackRegistry
Creates a deep copy of the registry with duplicated callable sets
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
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, | next unless Utils::Condition.evaluate(task, ) 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
72 73 74 75 76 77 78 |
# File 'lib/cmdx/callback_registry.rb', line 72 def register(type, *callables, **, &block) callables << block if block_given? registry[type] ||= Set.new registry[type] << [callables, ] self end |