Class: TypedOperation::PartiallyApplied

Inherits:
Object
  • Object
show all
Includes:
Operations::Composition
Defined in:
lib/typed_operation/partially_applied.rb

Overview

Represents an operation with some, but not all, required parameters provided. Allows incrementally building operation arguments before execution.

Direct Known Subclasses

Prepared

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Operations::Composition

#or_else, #then, #then_passes, #then_spreads, #transform

Constructor Details

#initialize(operation_class, *positional_args, **keyword_args) ⇒ PartiallyApplied

: (untyped, *untyped, **untyped) -> void



19
20
21
22
23
# File 'lib/typed_operation/partially_applied.rb', line 19

def initialize(operation_class, *positional_args, **keyword_args)
  @operation_class = operation_class
  @positional_args = positional_args
  @keyword_args = keyword_args
end

Instance Attribute Details

#keyword_argsObject (readonly)

: Hash[Symbol, untyped]



15
16
17
# File 'lib/typed_operation/partially_applied.rb', line 15

def keyword_args
  @keyword_args
end

#operation_classObject (readonly)

: untyped



16
17
18
# File 'lib/typed_operation/partially_applied.rb', line 16

def operation_class
  @operation_class
end

#positional_argsObject (readonly)

Returns the value of attribute positional_args.



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

def positional_args
  @positional_args
end

Instance Method Details

#callObject

: (*untyped, **untyped) -> untyped



46
47
48
49
50
# File 'lib/typed_operation/partially_applied.rb', line 46

def call(...)
  prepared = with(...)
  return prepared.operation.call if prepared.is_a?(Prepared)
  raise MissingParameterError, "Cannot call PartiallyApplied operation #{operation_class.name}, are you expecting it to be Prepared?"
end

#curryObject

: () -> Curried



41
42
43
# File 'lib/typed_operation/partially_applied.rb', line 41

def curry
  Curried.new(operation_class, self)
end

#deconstructObject

: () -> Array



68
69
70
# File 'lib/typed_operation/partially_applied.rb', line 68

def deconstruct
  positional_args + keyword_args.values
end

#deconstruct_keys(keys) ⇒ Object

: (Array?) -> Hash[Symbol, untyped]



73
74
75
76
77
# File 'lib/typed_operation/partially_applied.rb', line 73

def deconstruct_keys(keys)
  hash = keyword_args.dup
  positional_args.each_with_index { |value, index| hash[positional_parameters[index]] = value }
  keys ? hash.slice(*keys) : hash
end

#operationObject

: () -> untyped



53
54
55
# File 'lib/typed_operation/partially_applied.rb', line 53

def operation
  raise MissingParameterError, "Cannot instantiate Operation #{operation_class.name}, as it is only partially applied."
end

#prepared?Boolean

: () -> false

Returns:

  • (Boolean)


58
59
60
# File 'lib/typed_operation/partially_applied.rb', line 58

def prepared?
  false
end

#to_procObject

: () -> Proc



63
64
65
# File 'lib/typed_operation/partially_applied.rb', line 63

def to_proc
  method(:call).to_proc
end

#with(*positional, **keyword) ⇒ Object Also known as: []

: (*untyped, **untyped) -> (PartiallyApplied | Prepared)



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/typed_operation/partially_applied.rb', line 26

def with(*positional, **keyword)
  all_positional = positional_args + positional
  all_kw_args = keyword_args.merge(keyword)

  validate_positional_arg_count!(all_positional.size)

  if partially_applied?(all_positional, all_kw_args)
    PartiallyApplied.new(operation_class, *all_positional, **all_kw_args)
  else
    Prepared.new(operation_class, *all_positional, **all_kw_args)
  end
end