Module: Dry::Transaction::InstanceMethods

Includes:
Monads::Result::Mixin
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)

Raises:

  • (NotImplementedError)


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

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

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

  operation.(*args, &block)
end

Instance Attribute Details

#listenersObject (readonly)

Returns the value of attribute listeners.



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

def listeners
  @listeners
end

#operationsObject (readonly)

Returns the value of attribute operations.



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

def operations
  @operations
end

#stackObject (readonly)

Returns the value of attribute stack.



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

def stack
  @stack
end

#stepsObject (readonly)

Returns the value of attribute steps.



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

def steps
  @steps
end

Instance Method Details

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



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

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



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

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



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

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



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

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