Sha256: c8b8f1d4b38fb1ad4b6bcd44388e3a1279bd0de23bc2b1ef4dc2cf0ee331e1ec

Contents?: true

Size: 931 Bytes

Versions: 1

Compression:

Stored size: 931 Bytes

Contents

# frozen_string_literal: true

require 'micro/case'

module Micro
  class Case
    include Micro::Attributes::Features::ActiveModelValidations

    def self.auto_validation_disabled?
      @disable_auto_validation
    end

    def self.disable_auto_validation
      @disable_auto_validation = true
    end

    def initialize(input)
      @__input = input
      self.attributes = input
      run_validations! if respond_to?(:run_validations!, true)
    end

    private

      def __call_use_case
        return failure_by_validation_error(self) if !self.class.auto_validation_disabled? && invalid?

        result = call!

        return result if result.is_a?(Result)

        raise Error::UnexpectedResult.new(self.class)
      end

      def failure_by_validation_error(object)
        errors = object.respond_to?(:errors) ? object.errors : object

        Failure(:validation_error) { { errors: errors } }
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
u-case-2.1.1 lib/micro/case/with_validation.rb