Class: Cuprum::Command
- Inherits:
-
Object
- Object
- Cuprum::Command
- Extended by:
- SleepingKingStudios::Tools::Toolbox::Subclass
- Includes:
- Currying, Processing, Steps
- Defined in:
- lib/cuprum/command.rb
Overview
Functional object that encapsulates a business logic operation or step.
Using commands allows the developer to maintain a state or context, such as by passing context into the constructor. It provides a consistent interface by always returning a Cuprum::Result object, which tracks the status of the command call, the returned value, and the error object (if any). Finally, as a full-fledged Ruby object a Command can be passed around like any other object, including returned from a method (or another Command) or passed in as a parameter.
A Command can be defined either by passing a block to the constructor, or by defining a subclass of Command and implementing the #process method.
Direct Known Subclasses
BuiltIn::IdentityCommand, BuiltIn::NullCommand, Cuprum::Currying::CurriedCommand, MapCommand, Operation
Class Method Summary collapse
-
.subclass(*class_arguments, **class_keywords) { ... } ⇒ Class
Creates a subclass with partially applied constructor parameters.
Instance Method Summary collapse
-
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Result
Executes the command and returns a Cuprum::Result or compatible object.
-
#initialize {|arguments, keywords, block| ... } ⇒ Command
constructor
Returns a new instance of Cuprum::Command.
-
#process(*arguments, **keywords) { ... } ⇒ Cuprum::Result, Object
The implementation of the command.
-
#to_proc ⇒ Proc
Wraps the command in a proc.
Methods included from Steps
Methods included from Currying
Methods included from Processing
Constructor Details
#initialize {|arguments, keywords, block| ... } ⇒ Command
Returns a new instance of Cuprum::Command.
110 111 112 113 114 115 116 117 118 |
# File 'lib/cuprum/command.rb', line 110 def initialize(&implementation) return unless implementation define_singleton_method :process_block, &implementation singleton_class.send(:private, :process_block) @process_block = true end |
Class Method Details
.subclass(*class_arguments, **class_keywords) { ... } ⇒ Class
Creates a subclass with partially applied constructor parameters.
|
# File 'lib/cuprum/command.rb', line 81
|
Instance Method Details
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Result
121 122 123 |
# File 'lib/cuprum/command.rb', line 121 def call(*args, **kwargs, &) steps { super } end |
#process(*arguments, **keywords) { ... } ⇒ Cuprum::Result, Object
148 149 150 |
# File 'lib/cuprum/command.rb', line 148 def process(...) process_block? ? process_block(...) : super end |
#to_proc ⇒ Proc
Wraps the command in a proc.
Calling the proc will call the command with the given arguments, keywords, and block.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/cuprum/command.rb', line 131 def to_proc command = self @to_proc ||= lambda do |*args, **kwargs, &block| if kwargs.empty? command.call(*args, &block) else command.call(*args, **kwargs, &block) end end end |