Sha256: 1a7763399ddc4a0d0cf080baf12050370f8a46d6bf5d487afb4f9dfd95dc446b

Contents?: true

Size: 983 Bytes

Versions: 6

Compression:

Stored size: 983 Bytes

Contents

module Metaractor
  module ContextErrors
    def errors
      if super.nil?
        self.errors = Metaractor::Errors.new
      end

      super
    end

    def fail_with_error!(message: nil, errors: nil, **args)
      add_error(message: message, errors: errors, **args)
      fail!
    end

    def fail_with_errors!(messages: [], errors: {}, **args)
      add_errors(messages: messages, errors: errors, **args)
      fail!
    end

    def add_error(message: nil, errors: nil, **args)
      if message.nil?
        add_errors(errors: errors, **args)
      else
        add_errors(messages: Array(message), **args)
      end
    end

    def add_errors(messages: [], errors: {}, **args)
      if !messages.empty?
        self.errors.add(errors: { base: messages }, **args)
      else
        self.errors.add(errors: errors, **args)
      end
    end

    def error_messages
      errors.full_messages
    end
  end
end

Interactor::Context.send(:include, Metaractor::ContextErrors)

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
metaractor-3.3.0 lib/metaractor/context_errors.rb
metaractor-3.2.0 lib/metaractor/context_errors.rb
metaractor-3.1.1 lib/metaractor/context_errors.rb
metaractor-3.1.0 lib/metaractor/context_errors.rb
metaractor-3.0.1 lib/metaractor/context_errors.rb
metaractor-3.0.0 lib/metaractor/context_errors.rb