Sha256: 96bd1633efe5f46ad35421a6455cdeef6b686aa5c23515a4d7bf979beff14a1d

Contents?: true

Size: 1.29 KB

Versions: 9

Compression:

Stored size: 1.29 KB

Contents

# typed: true
# frozen_string_literal: true

require 'rider_kick/entities/failure_details'
require 'dry-matcher'
require 'dry/monads/all'

module RiderKick
  module Matchers
    class UseCaseResult
      def self.call(result, &block)
        new.matcher.call(result, &block)
      end

      def matcher
        Dry::Matcher.new(success: success_case, failure: failure_case)
      end

      private

      def success_case
        Dry::Matcher::Case.new(
          match:   ->(value) { value.is_a?(Dry::Monads::Success) },
          resolve: ->(value) { value.value! }
        )
      end

      def failure_case
        Dry::Matcher::Case.new(
          match:   ->(value) { value.is_a?(Dry::Monads::Failure) },
          resolve: ->(value) { resolve_failure_value(value) }
        )
      end

      def resolve_failure_value(value)
        failure = value.failure
        case failure
        when Array
          Entities::FailureDetails.from_array(failure)
        when String
          Entities::FailureDetails.from_string(failure)
        when Entities::FailureDetails
          failure
        else
          type_list = [Array, String, Entities::FailureDetails].map(&:to_s).join(' or ')
          raise ArgumentError, "Unexpected failure value - must be #{type_list}"
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rider-kick-0.0.9 lib/rider_kick/matchers/use_case_result.rb
rider-kick-0.0.8 lib/rider_kick/matchers/use_case_result.rb
rider-kick-0.0.7 lib/rider_kick/matchers/use_case_result.rb
rider-kick-0.0.6 lib/rider_kick/matchers/use_case_result.rb
rider-kick-0.0.5 lib/rider_kick/matchers/use_case_result.rb
rider-kick-0.0.4 lib/rider_kick/matchers/use_case_result.rb
rider-kick-0.0.3 lib/rider_kick/matchers/use_case_result.rb
rider-kick-0.0.2 lib/rider_kick/matchers/use_case_result.rb
rider-kick-0.0.1 lib/rider_kick/matchers/use_case_result.rb