Class: TypedOperation::Curried

Inherits:
Object
  • Object
show all
Defined in:
lib/typed_operation/curried.rb

Overview

Wraps an operation to support automatic currying, allowing parameters to be provided one at a time. Returns a new Curried instance until all required parameters are satisfied, then executes the operation.

Instance Method Summary collapse

Constructor Details

#initialize(operation_class, partial_operation = nil) ⇒ Curried

: (untyped, ?(PartiallyApplied | Prepared)) -> void



13
14
15
16
# File 'lib/typed_operation/curried.rb', line 13

def initialize(operation_class, partial_operation = nil)
  @operation_class = operation_class
  @partial_operation = partial_operation || operation_class.with
end

Instance Method Details

#call(arg) ⇒ Object

: (untyped) -> (Curried | untyped)

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/typed_operation/curried.rb', line 19

def call(arg)
  raise ArgumentError, "A prepared operation should not be curried" if @partial_operation.prepared?

  next_partially_applied = if next_parameter_positional?
    @partial_operation.with(arg)
  else
    key = next_keyword_parameter or raise ArgumentError, "No keyword parameter available"
    @partial_operation.with(key => arg)
  end
  if next_partially_applied.prepared?
    next_partially_applied.call
  else
    Curried.new(@operation_class, next_partially_applied)
  end
end

#to_procObject

: () -> Proc



36
37
38
# File 'lib/typed_operation/curried.rb', line 36

def to_proc
  method(:call).to_proc
end