Sha256: d1c722e579303899b0d55e667f38886cce187fd6f9050236c54a54a9f12d5cd0

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

require 'dry-matcher'

module CleanArchitecture
  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 String
          Entities::FailureDetails.new(message: failure, other_properties: {}, type: 'error')
        when Entities::FailureDetails
          failure
        else
          raise 'Unexpected failure value - must be String or Entities::FailureDetails'
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
clean-architecture-1.1.1 lib/clean_architecture/matchers/use_case_result.rb
clean-architecture-1.1.0 lib/clean_architecture/matchers/use_case_result.rb
clean-architecture-1.0.1 lib/clean_architecture/matchers/use_case_result.rb
clean-architecture-1.0.0 lib/clean_architecture/matchers/use_case_result.rb
clean-architecture-0.2.0 lib/clean_architecture/matchers/use_case_result.rb