Module: Cuprum::Chaining
- Included in:
- Command
- Defined in:
- lib/cuprum/chaining.rb
Overview
Mixin to implement command chaining functionality for a command class. Chaining commands allows you to define complex logic by composing it from simpler commands, including branching logic and error handling.
Instance Method Summary collapse
-
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Result
Executes the command implementation and returns a Cuprum::Result or compatible object.
-
#chain(command = nil, on: nil, &block) ⇒ Cuprum::Chaining
Creates a copy of the first command, and then chains the given command or block to execute after the first command’s implementation.
-
#chain!(command = nil, on: nil, &block) ⇒ Cuprum::Chaining
As #chain, but modifies the current command instead of creating a clone.
-
#tap_result(on: nil) {|result| ... } ⇒ Cuprum::Chaining
As #yield_result, but always returns the previous result when the block is called.
-
#tap_result!(on: nil) {|result| ... } ⇒ Cuprum::Chaining
As #tap_result, but modifies the current command instead of creating a clone.
-
#yield_result(on: nil) {|result| ... } ⇒ Cuprum::Chaining
Creates a copy of the command, and then chains the block to execute after the command implementation.
-
#yield_result!(on: nil) {|result| ... } ⇒ Cuprum::Chaining
As #yield_result, but modifies the current command instead of creating a clone.
Instance Method Details
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Result
200 201 202 |
# File 'lib/cuprum/chaining.rb', line 200 def call(*args, &block) yield_chain(super) end |
#chain(command, on: nil) ⇒ Cuprum::Chaining #chain(on: nil) {|value| ... } ⇒ Cuprum::Chaining
Creates a copy of the first command, and then chains the given command or block to execute after the first command’s implementation. When #call is executed, each chained command will be called with the previous result value, and its result property will be set to the previous result. The return value will be wrapped in a result and returned or yielded to the next block.
242 243 244 |
# File 'lib/cuprum/chaining.rb', line 242 def chain(command = nil, on: nil, &block) clone.chain!(command, on: on, &block) end |
#chain!(command, on: nil) ⇒ Cuprum::Chaining #chain!(on: nil) {|value| ... } ⇒ Cuprum::Chaining
As #chain, but modifies the current command instead of creating a clone. This is a protected method, and is meant to be called by the command to be chained, such as during #initialize.
321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/cuprum/chaining.rb', line 321 def chain!(command = nil, on: nil, &block) command ||= Cuprum::Command.new(&block) chained_procs << { proc: chain_command(command), on: on } # end hash self end |
#tap_result(on: nil) {|result| ... } ⇒ Cuprum::Chaining
As #yield_result, but always returns the previous result when the block is called. The return value of the block is discarded.
256 257 258 |
# File 'lib/cuprum/chaining.rb', line 256 def tap_result(on: nil, &block) clone.tap_result!(on: on, &block) end |
#tap_result!(on: nil) {|result| ... } ⇒ Cuprum::Chaining
As #tap_result, but modifies the current command instead of creating a clone. This is a protected method, and is meant to be called by the command to be chained, such as during #initialize.
350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/cuprum/chaining.rb', line 350 def tap_result!(on: nil, &block) tapped = ->(result) { result.tap { block.call(result) } } chained_procs << { proc: tapped, on: on } # end hash self end |
#yield_result(on: nil) {|result| ... } ⇒ Cuprum::Chaining
Creates a copy of the command, and then chains the block to execute after the command implementation. When #call is executed, each chained block will be yielded the previous result, and the return value wrapped in a result and returned or yielded to the next block.
279 280 281 |
# File 'lib/cuprum/chaining.rb', line 279 def yield_result(on: nil, &block) clone.yield_result!(on: on, &block) end |
#yield_result!(on: nil) {|result| ... } ⇒ Cuprum::Chaining
As #yield_result, but modifies the current command instead of creating a clone. This is a protected method, and is meant to be called by the command to be chained, such as during #initialize.
375 376 377 378 379 380 381 382 383 |
# File 'lib/cuprum/chaining.rb', line 375 def yield_result!(on: nil, &block) chained_procs << { proc: block, on: on } # end hash self end |