Sha256: 0ee4cd96e3e0971f7d5272c8849aef48122683becaf31a98a63cf2141c099e83

Contents?: true

Size: 802 Bytes

Versions: 2

Compression:

Stored size: 802 Bytes

Contents

# frozen_string_literal: true

module MuchRails; end

class MuchRails::ServiceValidationErrors
  attr_reader :hash

  def initialize
    @hash = {}
  end

  def add(exception_class, &block)
    unless exception_class < Exception
      raise(ArgumentError, "#{exception_class} is not an Exception")
    end

    @hash[exception_class] = block
  end

  def exception_classes
    @hash.keys
  end

  def result_for(ex)
    result_proc = nil
    exception_class = ex.class
    loop do
      result_proc = @hash[exception_class]
      break unless result_proc.nil?

      exception_class =
        if exception_class.superclass.nil?
          raise ArgumentError, "#{ex.class} hasn't been configured"
        else
          exception_class.superclass
        end
    end

    result_proc.call(ex)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
much-rails-0.2.1 lib/much-rails/service_validation_errors.rb
much-rails-0.2.0 lib/much-rails/service_validation_errors.rb