An opinionated implementation of the Command pattern for Ruby applications. Cuprum wraps your business logic in a consistent, object-oriented interface and features status and error management, composability and control flow management.
Creates a subclass with partially applied constructor parameters.
Parameters
class_arguments (Array) — the arguments, if any, to apply to the
constructor. These arguments will be added before any args passed
directly to the constructor.
class_keywords (Hash) — the keywords, if any, to apply to the
constructor. These keywords will be added before any kwargs passed
directly to the constructor.
Yields
the block, if any, to pass to the constructor. This will be
overriden by a block passed directly to the constructor.
Returns an indication of the number of arguments accepted by #call.
If the method takes a fixed number N of arguments, returns N. If the
method takes a variable number of arguments, returns -N-1, where N is the
number of required arguments. Keyword arguments will be considered as a
single additional argument, that argument being mandatory if any keyword
argument is mandatory.
Clears the reference to the most recent call of the operation, if any.
This allows the result and any referenced data to be garbage collected.
Use this method to clear any instance variables or state internal to the
operation (an operation should never have external state apart from the
last result).
If the operation cannot be run more than once, this method should raise
an error.
Executes the block and returns the value, or halts on a failure.
The #step method is used to evaluate a sequence of processes, and to
fail fast and halt processing if any of the steps returns a failing
result. Each invocation of #step should be wrapped in a #steps block,
or used inside the #process method of a Command.
If the object returned by the block is a Cuprum result or compatible
object (such as a called operation), the value is converted to a Cuprum
result via the #to_cuprum_result method. Otherwise, the object is
returned directly from #step.
If the returned object is a passing result, the #value of the result is
returned by #step.
If the returned object is a failing result, then #step will throw
:cuprum_failed_result and the failing result. This is caught by the
#steps block, and halts execution of any subsequent steps.
Examples
Calling a Step
Calling a Step with a Passing Result
Calling a Step with a Failing Result
Yields
Called with no parameters.
Returns
(Object) — the #value of the result, or the returned object.
Returns the first failing #step result, or the final result if none fail.
The #steps method is used to wrap a series of #step calls. Each step is
executed in sequence. If any of the steps returns a failing result, that
result is immediately returned from #steps. Otherwise, #steps wraps the
value returned by a block in a Cuprum result.
Examples
With A Passing Step
With A Failing Step
With Multiple Steps
Yields
Called with no parameters.
Yield Returns
(Cuprum::Result, Object) — a Cuprum result, or an object to be
wrapped in a result.
Returns
(Cuprum::Result) — the result or object returned by the block,
wrapped in a Cuprum result.
(Cuprum::Result) — the most recent result if the operation was
previously called; otherwise, returns a failing result with a
Cuprum::Errors::OperationNotCalled error.