Class: Cuprum::Currying::CurriedCommand

Inherits:
Cuprum::Command show all
Defined in:
lib/cuprum/currying/curried_command.rb

Overview

A CurriedCommand wraps another command and passes preset params to #call.

Examples:

Currying Arguments

# Our base command takes two arguments.
say_command = Cuprum::Command.new do |greeting, person|
  "#{greeting}, #{person}!"
end
say_command.call('Hello', 'world')
#=> returns a result with value 'Hello, world!'

# Next, we create a curried command. This sets the first argument to
# always be 'Greetings', so our curried command only takes one argument,
# namely the name of the person being greeted.
greet_command =
  Cuprum::CurriedCommand.new(
    arguments: ['Greetings'],
    command:   say_command
  )
greet_command.call('programs')
#=> returns a result with value 'Greetings, programs!'

# Here, we are creating a curried command that passes both arguments.
# Therefore, our curried command does not take any arguments.
recruit_command =
  Cuprum::CurriedCommand.new(
    arguments: ['Greetings', 'starfighter'],
    command:   say_command
  )
recruit_command.call
#=> returns a result with value 'Greetings, starfighter!'

Currying Keywords

# Our base command takes two keywords: a math operation and an array of
# integers.
math_command = Cuprum::Command.new do |operands:, operation:|
  operations.reduce(&operation)
end
math_command.call(operands: [2, 2], operation: :+)
#=> returns a result with value 4

# Our curried command still takes two keywords, but now the operation
# keyword is optional. It now defaults to :*, for multiplication.
multiply_command =
  Cuprum::CurriedCommand.new(
    command:  math_command,
    keywords: { operation: :* }
  )
multiply_command.call(operands: [3, 3])
#=> returns a result with value 9

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Cuprum::Command

#to_proc

Methods included from Steps

#step, #steps

Methods included from Cuprum::Currying

#curry

Methods included from Processing

#arity

Constructor Details

#initialize(command:, arguments: [], block: nil, keywords: {}) { ... } ⇒ CurriedCommand

Returns a new instance of CurriedCommand.

Parameters:

  • arguments (Array) (defaults to: [])

    The arguments to pass to the curried command.

  • command (Cuprum::Command)

    The original command to curry.

  • keywords (Hash) (defaults to: {})

    The keywords to pass to the curried command.

Yields:

  • A block to pass to the curried command.



60
61
62
63
64
65
66
67
# File 'lib/cuprum/currying/curried_command.rb', line 60

def initialize(command:, arguments: [], block: nil, keywords: {})
  super()

  @arguments = arguments
  @block     = block
  @command   = command
  @keywords  = keywords
end

Instance Attribute Details

#argumentsArray (readonly)

Returns the arguments to pass to the curried command.

Returns:

  • (Array)

    the arguments to pass to the curried command.



92
93
94
# File 'lib/cuprum/currying/curried_command.rb', line 92

def arguments
  @arguments
end

#blockProc? (readonly)

Returns a block to pass to the curried command.

Returns:

  • (Proc, nil)

    a block to pass to the curried command.



95
96
97
# File 'lib/cuprum/currying/curried_command.rb', line 95

def block
  @block
end

#commandCuprum::Command (readonly)

Returns the original command to curry.

Returns:



98
99
100
# File 'lib/cuprum/currying/curried_command.rb', line 98

def command
  @command
end

#keywordsHash (readonly)

Returns the keywords to pass to the curried command.

Returns:

  • (Hash)

    the keywords to pass to the curried command.



101
102
103
# File 'lib/cuprum/currying/curried_command.rb', line 101

def keywords
  @keywords
end

Instance Method Details

#call(*args, **kwargs) ⇒ Cuprum::Result

Merges the arguments and keywords and calls the wrapped command.

First, the arguments array is created starting with the :arguments passed to #initialize. Any positional arguments passed directly to #call are then appended.

Second, the keyword arguments are created by merging the keywords passed directly into #call into the keywods passed to #initialize. This means that if a key is passed in both places, the value passed into #call will take precedence.

Finally, the merged arguments and keywords are passed into the original command’s #call method.

Parameters:

  • args (Array)

    Additional arguments to pass to the curried command.

  • kwargs (Hash)

    Additional keywords to pass to the curried command.

Returns:

See Also:



# File 'lib/cuprum/currying/curried_command.rb', line 69