Module: Cuprum::Processing

Includes:
ResultHelpers
Included in:
Command
Defined in:
lib/cuprum/processing.rb

Overview

Functional implementation for creating a command object. Cuprum::Processing defines a #call method, which performs the implementation defined by #process and returns an instance of Cuprum::Result.

Examples:

Defining a command with Cuprum::Processing.

class AdderCommand
  include Cuprum::Processing

  def initialize addend
    @addend = addend
  end

  private

  def process int
    int + addend
  end
end

adder  = AdderCommand.new(2)
result = adder.call(3)
#=> an instance of Cuprum::Result
result.value    #=> 5
result.success? #=> true

Defining a command with error handling.

class SquareRootCommand
  include Cuprum::Processing

  private

  def process value
    if value.negative?
      return Cuprum::Result.new(error: 'value cannot be negative')
    end

    Math.sqrt(value)
  end
end

result = SquareRootCommand.new.call(2)
result.value    #=> 1.414
result.success? #=> true
result.failure? #=> false
result.error    #=> nil

result = SquareRootCommand.new.call(-1)
result.value    #=> nil
result.success? #=> false
result.failure? #=> true
result.error    #=> 'value cannot be negative'

See Also:

Instance Method Summary collapse

Instance Method Details

#arityInteger

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.

Returns:

  • (Integer)

    The number of arguments.

See Also:

  • Method#arity.


74
75
76
# File 'lib/cuprum/processing.rb', line 74

def arity
  method(:process).arity
end

#call(*arguments, **keywords) { ... } ⇒ Cuprum::Result

Executes the command and returns a Cuprum::Result or compatible object.

Each time #call is invoked, the object performs the following steps:

  1. The #process method is called, passing the arguments, keywords, and block that were passed to #call.

  2. If the value returned by #process is a Cuprum::Result or compatible object, that result is directly returned by #call.

  3. Otherwise, the value returned by #process will be wrapped in a successful result, which will be returned by #call.

Parameters:

  • arguments (Array)

    Arguments to be passed to the implementation.

  • keywords (Hash)

    Keywords to be passed to the implementation.

Yields:

  • If a block argument is given, it will be passed to the implementation.

Returns:



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cuprum/processing.rb', line 98

def call(*args, **kwargs, &block)
  value =
    if kwargs.empty?
      process(*args, &block)
    else
      process(*args, **kwargs, &block)
    end

  return value.to_cuprum_result if value_is_result?(value)

  build_result(value: value)
end

#process(*arguments, **keywords) { ... } ⇒ Cuprum::Result, Object

Note:

This is a private method.

The implementation of the command.

Whereas the #call method provides the public interface for calling a command, the #process method defines the actual implementation. This method should not be called directly.

When the command is called via #call, the parameters are passed to #process. If #process returns a result, that result will be returned by #call; otherwise, the value returned by #process will be wrapped in a successful Cuprum::Result object.

Parameters:

  • arguments (Array)

    The arguments, if any, passed from #call.

  • keywords (Hash)

    The keywords, if any, passed from #call.

Yields:

  • The block, if any, passed from #call.

Returns:

  • (Cuprum::Result, Object)

    a result object, or the value of the result object to be returned by #call.



136
137
138
139
140
# File 'lib/cuprum/processing.rb', line 136

def process(*_args)
  error = Cuprum::Errors::CommandNotImplemented.new(command: self)

  build_result(error: error)
end