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.
-
#dup ⇒ Hanami::Utils::Callbacks
Return a duplicate callbacks 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
25 26 27 |
# File 'lib/hanami/utils/callbacks.rb', line 25 def initialize @chain = Concurrent::Array.new end |
Instance Method Details
#append(*callbacks, &block) ⇒ void
This method returns an undefined value.
Appends the given callbacks to the end of the chain.
60 61 62 63 64 65 66 |
# File 'lib/hanami/utils/callbacks.rb', line 60 def append(*callbacks, &block) callables(callbacks, block).each do |c| @chain.push(c) end @chain.uniq! end |
#dup ⇒ Hanami::Utils::Callbacks
Return a duplicate callbacks chain
163 164 165 166 167 |
# File 'lib/hanami/utils/callbacks.rb', line 163 def dup super.tap do |instance| instance.instance_variable_set(:@chain, instance.chain.dup) end end |
#freeze ⇒ Object
It freezes the object by preventing further modifications.
184 185 186 187 |
# File 'lib/hanami/utils/callbacks.rb', line 184 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.
99 100 101 102 103 104 105 |
# File 'lib/hanami/utils/callbacks.rb', line 99 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`.
152 153 154 155 156 |
# File 'lib/hanami/utils/callbacks.rb', line 152 def run(context, *args) @chain.each do |callback| callback.call(context, *args) end end |