lib/dry/transaction/result_matcher.rb in dry-transaction-0.7.0 vs lib/dry/transaction/result_matcher.rb in dry-transaction-0.8.0

- old
+ new

@@ -1,30 +1,22 @@ +require "dry-matcher" + module Dry module Transaction - class ResultMatcher - attr_reader :result - attr_reader :output - - def initialize(result) - if result.respond_to?(:to_either) - @result = result.to_either - else - @result = result - end - end - - def success(&block) - return output unless result.right? - - @output = block.call(result.value) - end - - def failure(step_name = nil, &block) - return output unless result.left? - - if step_name.nil? || step_name == result.value.__step_name - @output = block.call(result.value) - end - end - end + ResultMatcher = Dry::Matcher.new( + success: Dry::Matcher::Case.new( + match: -> result { result.right? }, + resolve: -> result { result.value }, + ), + failure: Dry::Matcher::Case.new( + match: -> result, step_name = nil { + if step_name + result.left? && result.value.step_name == step_name + else + result.left? + end + }, + resolve: -> result { result.value.value }, + ) + ) end end