Class: NxtPipeline::Callbacks
- Inherits:
-
Object
- Object
- NxtPipeline::Callbacks
- Defined in:
- lib/nxt_pipeline/callbacks.rb
Instance Method Summary collapse
- #around(type, change_set, &execution) ⇒ Object
-
#initialize(pipeline:) ⇒ Callbacks
constructor
A new instance of Callbacks.
- #register(path, callback) ⇒ Object
- #run(kind_of_callback, type, change_set) ⇒ Object
Constructor Details
#initialize(pipeline:) ⇒ Callbacks
Returns a new instance of Callbacks.
3 4 5 6 |
# File 'lib/nxt_pipeline/callbacks.rb', line 3 def initialize(pipeline:) @registry = build_registry @pipeline = pipeline end |
Instance Method Details
#around(type, change_set, &execution) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/nxt_pipeline/callbacks.rb', line 21 def around(type, change_set, &execution) around_callbacks = registry.resolve!(:around, type) return execution.call unless around_callbacks.any? callback_chain = around_callbacks.reverse.inject(execution) do |previous, callback| -> { callback.call(change_set, previous, pipeline) } end callback_chain.call end |
#register(path, callback) ⇒ Object
8 9 10 |
# File 'lib/nxt_pipeline/callbacks.rb', line 8 def register(path, callback) registry.resolve!(*path) << callback end |
#run(kind_of_callback, type, change_set) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/nxt_pipeline/callbacks.rb', line 12 def run(kind_of_callback, type, change_set) callbacks = registry.resolve!(kind_of_callback, type) return unless callbacks.any? callbacks.inject(change_set) do |changes, callback| run_callback(callback, changes) end end |