Class: TypedOperation::PartiallyApplied

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

Direct Known Subclasses

Prepared

Instance Method Summary collapse

Constructor Details

#initialize(operation, **applied_args) ⇒ PartiallyApplied

Returns a new instance of PartiallyApplied.



5
6
7
8
# File 'lib/typed_operation/partially_applied.rb', line 5

def initialize(operation, **applied_args)
  @operation = operation
  @applied_args = applied_args
end

Instance Method Details

#callObject



29
30
31
32
33
# File 'lib/typed_operation/partially_applied.rb', line 29

def call(...)
  prepared = curry(...)
  return prepared.operation.call if prepared.is_a?(Prepared)
  raise TypedOperation::MissingParameterError, "Cannot call PartiallyApplied operation #{@operation.name} (key: #{@operation.operation_key}), are you expecting it to be Prepared?"
end

#curry(**params) ⇒ Object Also known as: [], with



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/typed_operation/partially_applied.rb', line 10

def curry(**params)
  all_args = @applied_args.merge(params)
  # check if required attrs are in @applied_args
  required_keys = @operation.attribute_names.select do |name|
    meta = @operation.(name)
    meta[:required] != false && !meta[:typed_attribute_options].key?(:default)
  end
  missing_keys = required_keys - all_args.keys

  if missing_keys.size > 0
    # Partially apply the arguments
    PartiallyApplied.new(@operation, **all_args)
  else
    Prepared.new(@operation, **all_args)
  end
end