Module: Dry::Transaction::InstanceMethods

Defined in:
lib/dry/transaction/instance_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dry/transaction/instance_methods.rb', line 75

def method_missing(name, *args, &block)
  step = steps.detect { |s| s.name == name }
  super unless step

  operation = operations[step.name]
  unless operation
    raise NotImplementedError,
          "no operation +#{step.operation_name}+ defined for step +#{step.name}+"
  end

  operation.(*args, &block)
end

Instance Attribute Details

#listenersObject (readonly)

Returns the value of attribute listeners.



13
14
15
# File 'lib/dry/transaction/instance_methods.rb', line 13

def listeners
  @listeners
end

#operationsObject (readonly)

Returns the value of attribute operations.



12
13
14
# File 'lib/dry/transaction/instance_methods.rb', line 12

def operations
  @operations
end

#stackObject (readonly)

Returns the value of attribute stack.



14
15
16
# File 'lib/dry/transaction/instance_methods.rb', line 14

def stack
  @stack
end

#stepsObject (readonly)

Returns the value of attribute steps.



11
12
13
# File 'lib/dry/transaction/instance_methods.rb', line 11

def steps
  @steps
end

Instance Method Details

#call(input = nil, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dry/transaction/instance_methods.rb', line 26

def call(input = nil, &block)
  assert_step_arity

  result = stack.(Success(input))

  if block
    ResultMatcher.(result, &block)
  else
    result.or { |step_failure|
      # Unwrap the value from the StepFailure and return it directly
      Failure(step_failure.value)
    }
  end
end

#initialize(steps: self.class.steps, listeners: nil, **operations) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/dry/transaction/instance_methods.rb', line 16

def initialize(steps: self.class.steps, listeners: nil, **operations)
  @steps = steps.map { |step|
    operation = resolve_operation(step, **operations)
    step.with(operation: operation)
  }
  @operations = operations
  @stack = Stack.new(@steps)
  subscribe(listeners) unless listeners.nil?
end

#subscribe(listeners) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dry/transaction/instance_methods.rb', line 41

def subscribe(listeners)
  @listeners = listeners

  if listeners.is_a?(Hash)
    listeners.each do |step_name, listener|
      steps.detect { |step| step.name == step_name }.subscribe(listener)
    end
  else
    steps.each do |step|
      step.subscribe(listeners)
    end
  end
end

#with_step_args(**step_args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dry/transaction/instance_methods.rb', line 55

def with_step_args(**step_args)
  assert_valid_step_args(step_args)

  new_steps = steps.map { |step|
    if step_args[step.name]
      step.with(call_args: step_args[step.name])
    else
      step
    end
  }

  self.class.new(steps: new_steps, listeners: listeners, **operations)
end