Sha256: 202185ec6685c0f479ae991bfae0b44e0e1c68cef08fa04d7568078243ceb9c5

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module Lev

  # A collection of Error objects.
  #
  class Errors < Array

    def initialize(routine_job = nil, raise_fatal_errors = false)
      @routine_job = routine_job || NoBackgroundJob.new
      @raise_fatal_errors = raise_fatal_errors
    end

    def add(fail, args={})
      args[:kind] ||= :lev
      error = Error.new(args)

      return if ignored_error_procs.any?{|proc| proc.call(error)}
      self.push(error)

      routine_job.add_error(error, is_fatal: fail)

      if fail
        routine_job.failed!

        if raise_fatal_errors
          raise StandardError, args.to_a.map { |i| i.join(' ') }.join(' - ')
        else
          throw :fatal_errors_encountered
        end
      end
    end

    def ignore(arg)
      proc = arg.is_a?(Symbol) ?
               Proc.new{|error| error.code == arg} :
               arg

      raise Lev.configuration.illegal_argument_error if !proc.respond_to?(:call)

      ignored_error_procs.push(proc)
    end

    def [](key)
      self[key]
    end

    # Checks to see if the provided input is associated with one of the errors.
    def has_offending_input?(input)
      self.any? {|error| [error.offending_inputs].flatten.include? input}
    end

    def raise_exception_if_any!(exception_type = StandardError)
      raise exception_type, collect{|error| error.message}.join('; ') if any?
    end

  protected

    attr_reader :routine_job
    attr_reader :raise_fatal_errors

    def ignored_error_procs
      @ignored_error_procs ||= []
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lev-5.0.0 lib/lev/errors.rb