Class: Hanami::Utils::Callbacks::Chain
- Inherits:
-
Object
- Object
- Hanami::Utils::Callbacks::Chain
- Defined in:
- lib/hanami/utils/callbacks.rb
Overview
Series of callbacks to be executed
Instance Method Summary collapse
-
#append(*callbacks, &block) ⇒ void
Appends the given callbacks to the end of the chain.
-
#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/hanami/utils/callbacks.rb', line 20 def initialize @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.
55 56 57 58 59 60 61 |
# File 'lib/hanami/utils/callbacks.rb', line 55 def append(*callbacks, &block) callables(callbacks, block).each do |c| @chain.push(c) end @chain.uniq! end |
#freeze ⇒ Object
It freezes the object by preventing further modifications.
168 169 170 171 |
# File 'lib/hanami/utils/callbacks.rb', line 168 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.
94 95 96 97 98 99 100 |
# File 'lib/hanami/utils/callbacks.rb', line 94 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`.
147 148 149 150 151 |
# File 'lib/hanami/utils/callbacks.rb', line 147 def run(context, *args) @chain.each do |callback| callback.call(context, *args) end end |