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
22 23 24 |
# File 'lib/hanami/utils/callbacks.rb', line 22 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.
57 58 59 60 61 62 63 |
# File 'lib/hanami/utils/callbacks.rb', line 57 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
160 161 162 163 164 |
# File 'lib/hanami/utils/callbacks.rb', line 160 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.
181 182 183 184 |
# File 'lib/hanami/utils/callbacks.rb', line 181 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.
96 97 98 99 100 101 102 |
# File 'lib/hanami/utils/callbacks.rb', line 96 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.
149 150 151 152 153 |
# File 'lib/hanami/utils/callbacks.rb', line 149 def run(context, *args) @chain.each do |callback| callback.call(context, *args) end end |