Class: Hanami::Utils::Callbacks::Chain
- Inherits:
-
Object
- Object
- Hanami::Utils::Callbacks::Chain
- Defined in:
- lib/idempotency/hanami/utils/callbacks.rb
Overview
Series of callbacks to be executed
Instance Attribute Summary collapse
- #chain ⇒ Object readonly
Instance Method Summary collapse
-
#append(*callbacks, &block) ⇒ void
Appends the given callbacks to the end of the chain.
- #append_around(&block) ⇒ Object
-
#freeze ⇒ Object
It freezes the object by preventing further modifications.
-
#initialize ⇒ Hanami::Utils::Callbacks::Chain
constructor
Returns a new chain.
-
#prepend(*callbacks, &block) ⇒ void
Prepends the given callbacks to the beginning of the chain.
-
#run(context, *args) ⇒ Object
Runs all the callbacks in the chain.
Constructor Details
#initialize ⇒ Hanami::Utils::Callbacks::Chain
Returns a new chain
20 21 22 |
# File 'lib/idempotency/hanami/utils/callbacks.rb', line 20 def initialize @chain = [] end |
Instance Attribute Details
#chain ⇒ Object (readonly)
24 25 26 |
# File 'lib/idempotency/hanami/utils/callbacks.rb', line 24 def chain @chain end |
Instance Method Details
#append(*callbacks, &block) ⇒ void
This method returns an undefined value.
Appends the given callbacks to the end of the chain.
57 58 59 60 61 62 63 |
# File 'lib/idempotency/hanami/utils/callbacks.rb', line 57 def append(*callbacks, &block) callables(callbacks, block).each do |c| @chain.push(c) end @chain.uniq! end |
#append_around(&block) ⇒ Object
65 66 67 |
# File 'lib/idempotency/hanami/utils/callbacks.rb', line 65 def append_around(&block) @chain.push(AroundCallback.new(block)) end |
#freeze ⇒ Object
It freezes the object by preventing further modifications.
174 175 176 177 |
# File 'lib/idempotency/hanami/utils/callbacks.rb', line 174 def freeze super @chain.freeze end |
#prepend(*callbacks, &block) ⇒ void
This method returns an undefined value.
Prepends the given callbacks to the beginning of the chain.
100 101 102 103 104 105 106 |
# File 'lib/idempotency/hanami/utils/callbacks.rb', line 100 def prepend(*callbacks, &block) callables(callbacks, block).each do |c| @chain.unshift(c) end @chain.uniq! end |
#run(context, *args) ⇒ Object
Runs all the callbacks in the chain. The only two ways to stop the execution are: ‘raise` or `throw`.
153 154 155 156 157 |
# File 'lib/idempotency/hanami/utils/callbacks.rb', line 153 def run(context, *args) @chain.each do |callback| callback.call(context, *args) end end |