Class: Cuprum::Command
- Inherits:
-
Object
- Object
- Cuprum::Command
- 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
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.
-
#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.
88 89 90 91 92 93 94 |
# File 'lib/cuprum/command.rb', line 88 def initialize(&implementation) return unless implementation define_singleton_method :process, &implementation singleton_class.send(:private, :process) end |
Instance Method Details
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Result
97 98 99 |
# File 'lib/cuprum/command.rb', line 97 def call(*args, **kwargs, &block) steps { 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.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/cuprum/command.rb', line 107 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 |