Sha256: bfba33b759035eeaa53d3d04a4bd7fa814f828ba206164b2d704581d2393babc

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# typed: true
# frozen_string_literal: true

require 'dry/monads/result'
require 'dry/validation'
require 'dry/matcher/result_matcher'
require 'clean_architecture/use_cases/errors'
require 'clean_architecture/use_cases/parameters'
require 'clean_architecture/use_cases/contract'
require 'clean_architecture/entities/failure_details'
require 'forwardable'

module CleanArchitecture
  module UseCases
    class AbstractUseCase
      extend Forwardable

      @contract = nil

      def self.contract(base_contract = Contract)
        @contract ||= begin
          Class.new(base_contract, &Proc.new)
        end
      end

      def self.parameters(params)
        raise 'You must define a contract first' if @contract.nil?

        context = params.fetch(:context, {})
        Parameters.new(
          context,
          contract.new(context).call(params)
        )
      end

      def initialize(params)
        @params = params
      end

      def result
        raise NotImplementedError
      end

      private

      DEFAULT_FAILURE_TYPE = 'error'

      def fail_with_error_message(message, failure_type = DEFAULT_FAILURE_TYPE)
        new_errors = Errors.new(nil, failure_type)
        new_errors.add(:base, message)
        Dry::Monads::Failure(new_errors)
      end

      def result_of_validating_params
        @params.to_monad
      end

      def context(key)
        @params.context(key)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clean-architecture-5.0.2 lib/clean_architecture/use_cases/abstract_use_case.rb