lib/dry/transaction/sequence.rb in dry-transaction-0.7.0 vs lib/dry/transaction/sequence.rb in dry-transaction-0.8.0
- old
+ new
@@ -1,18 +1,23 @@
-require "dry/transaction/result_matcher"
+require "dry/monads/either"
+require "dry/transaction/dsl"
module Dry
module Transaction
class Sequence
include Dry::Monads::Either::Mixin
# @api private
attr_reader :steps
# @api private
- def initialize(steps)
+ attr_reader :matcher
+
+ # @api private
+ def initialize(steps, matcher)
@steps = steps
+ @matcher = matcher
end
# Run the transaction.
#
# Each operation will be called in the order it was specified, with its
@@ -48,13 +53,16 @@
steps = steps_with_options_applied(options)
result = steps.inject(Right(input), :bind)
if block
- block.call(ResultMatcher.new(result))
+ matcher.(result, &block)
else
- result
+ result.or { |step_failure|
+ # Unwrap the value from the StepFailure and return it directly
+ Left(step_failure.value)
+ }
end
end
alias_method :[], :call
# Subscribe to notifications from steps.
@@ -134,11 +142,11 @@
#
# @api public
def prepend(other = nil, **options, &block)
other = accept_or_build_transaction(other, **options, &block)
- self.class.new(other.steps + steps)
+ self.class.new(other.steps + steps, matcher)
end
# Return a transaction with the steps from the provided transaction
# appended onto the end of the steps in `self`.
#
@@ -175,11 +183,11 @@
#
# @api public
def append(other = nil, **options, &block)
other = accept_or_build_transaction(other, **options, &block)
- self.class.new(steps + other.steps)
+ self.class.new(steps + other.steps, matcher)
end
# Return a transaction with the steps from the provided transaction
# inserted into a specific place among the steps in `self`.
#
@@ -228,11 +236,11 @@
end
other = accept_or_build_transaction(other, **options, &block)
index = steps.index { |step| step.step_name == insertion_step } + (!!after ? 1 : 0)
- self.class.new(steps.dup.insert(index, *other.steps))
+ self.class.new(steps.dup.insert(index, *other.steps), matcher)
end
# @overload remove(step, ...)
# Return a transaction with steps removed.
#
@@ -250,10 +258,10 @@
#
# @return [Dry::Transaction::Sequence] the modified transaction object
#
# @api public
def remove(*steps_to_remove)
- self.class.new(steps.reject { |step| steps_to_remove.include?(step.step_name) })
+ self.class.new(steps.reject { |step| steps_to_remove.include?(step.step_name) }, matcher)
end
private
def assert_valid_options(options)