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.
-
#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.
-
#yield_result(on: nil) {|result| ... } ⇒ Cuprum::Chaining
Creates a copy of the command, and then chains the block to execute after the command implementation.
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.
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/cuprum/chaining.rb', line 195 def chain command = nil, on: nil, &block command ||= Cuprum::Command.new(&block) clone.tap do |fn| fn.chained_procs << { :proc => chain_command(command), :on => on } # end hash end # tap 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.
225 226 227 |
# File 'lib/cuprum/chaining.rb', line 225 def failure command = nil, &block 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.
247 248 249 |
# File 'lib/cuprum/chaining.rb', line 247 def success command = nil, &block 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.
261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/cuprum/chaining.rb', line 261 def tap_result on: nil, &block tapped = ->(result) { result.tap { block.call(result) } } clone.tap do |fn| fn.chained_procs << { :proc => tapped, :on => on } # end hash end # tap 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.
293 294 295 296 297 298 299 300 301 |
# File 'lib/cuprum/chaining.rb', line 293 def yield_result on: nil, &block clone.tap do |fn| fn.chained_procs << { :proc => block, :on => on } # end hash end # tap end |