Module: Cuprum::Processing

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 a nonnegative integer for commands that take a fixed number of arguments. For commands that take a variable number of arguments, returns -n-1, where n is the number of required arguments.

Returns:

  • (Integer)

    The number of arguments.



65
66
67
# File 'lib/cuprum/processing.rb', line 65

def arity
  method(:process).arity
end

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

Executes the command implementation 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:



90
91
92
93
94
95
96
# File 'lib/cuprum/processing.rb', line 90

def call(*args, &block)
  value = process(*args, &block)

  return value.to_cuprum_result if value_is_result?(value)

  build_result(value: value)
end

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

Note:

This is a private method.

The implementation of the command, to be executed when the #call method is called. 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. This method should not be called directly.

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:

  • (Object)

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



125
126
127
128
129
# File 'lib/cuprum/processing.rb', line 125

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

  build_result(error: error)
end