Sha256: 25b1aa1d726c26d13688e1d52873dc85fa897ca51e291d4b8a87e6e14e4009e6

Contents?: true

Size: 1.06 KB

Versions: 9

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

require 'kind/active_model/validation'

require 'micro/case'

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

    def self.auto_validation_disabled?
      return @disable_auto_validation if defined?(@disable_auto_validation)
    end

    def self.disable_auto_validation
      @disable_auto_validation = true
    end

    def initialize(input)
      __setup_use_case(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.name}#call!")
      end

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

        Failure Micro::Case::Config.instance.activemodel_validation_errors_failure, result: {
          errors: errors
        }
      end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
u-case-4.3.0 lib/micro/case/with_activemodel_validation.rb
u-case-4.2.2 lib/micro/case/with_activemodel_validation.rb
u-case-4.2.1 lib/micro/case/with_activemodel_validation.rb
u-case-4.2.0 lib/micro/case/with_activemodel_validation.rb
u-case-4.1.1 lib/micro/case/with_activemodel_validation.rb
u-case-4.1.0 lib/micro/case/with_activemodel_validation.rb
u-case-4.0.0 lib/micro/case/with_activemodel_validation.rb
u-case-3.1.0 lib/micro/case/with_activemodel_validation.rb
u-case-3.0.0 lib/micro/case/with_activemodel_validation.rb