Class: Lotus::Utils::Callbacks::Chain
- Inherits:
-
Object
- Object
- Lotus::Utils::Callbacks::Chain
- Defined in:
- lib/lotus/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 ⇒ Lotus::Utils::Callbacks::Chain
constructor
Return 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 ⇒ Lotus::Utils::Callbacks::Chain
Return a new chain
18 19 20 |
# File 'lib/lotus/utils/callbacks.rb', line 18 def initialize @chain = 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.
53 54 55 56 57 58 59 |
# File 'lib/lotus/utils/callbacks.rb', line 53 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.
166 167 168 169 |
# File 'lib/lotus/utils/callbacks.rb', line 166 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.
92 93 94 95 96 97 98 |
# File 'lib/lotus/utils/callbacks.rb', line 92 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`.
145 146 147 148 149 |
# File 'lib/lotus/utils/callbacks.rb', line 145 def run(context, *args) @chain.each do |callback| callback.call(context, *args) end end |