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
-
#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.
-
#failure(command = nil, &block) ⇒ Cuprum::Chaining
Shorthand for command.chain(:on => :failure).
-
#success(command = nil, &block) ⇒ Cuprum::Chaining
Shorthand for command.chain(:on => :success).
-
#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
#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.
217 218 219 |
# File 'lib/cuprum/chaining.rb', line 217 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.
345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/cuprum/chaining.rb', line 345 def chain! command = nil, on: nil, &block command ||= Cuprum::Command.new(&block) chained_procs << { :proc => chain_command(command), :on => on } # end hash self end |
#failure(command) ⇒ Cuprum::Chaining #failure {|value| ... } ⇒ Cuprum::Chaining
Shorthand for command.chain(:on => :failure). Creates a copy of the first command, and then chains the given command or block to execute after the first command’s implementation, but only if the previous command is failing.
239 240 241 |
# File 'lib/cuprum/chaining.rb', line 239 def failure command = nil, &block clone.chain!(command, :on => :failure, &block) end |
#success(command) ⇒ Cuprum::Chaining #success {|value| ... } ⇒ Cuprum::Chaining
Shorthand for command.chain(:on => :success). Creates a copy of the first command, and then chains the given command or block to execute after the first command’s implementation, but only if the previous command is failing.
261 262 263 |
# File 'lib/cuprum/chaining.rb', line 261 def success command = nil, &block clone.chain!(command, :on => :success, &block) 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.
275 276 277 |
# File 'lib/cuprum/chaining.rb', line 275 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.
378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/cuprum/chaining.rb', line 378 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.
299 300 301 |
# File 'lib/cuprum/chaining.rb', line 299 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.
403 404 405 406 407 408 409 410 411 |
# File 'lib/cuprum/chaining.rb', line 403 def yield_result! on: nil, &block chained_procs << { :proc => block, :on => on } # end hash self end |