Class: MuchRails::ServiceValidationErrors

Inherits:
Object
  • Object
show all
Defined in:
lib/much-rails/service_validation_errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServiceValidationErrors

Returns a new instance of ServiceValidationErrors.



8
9
10
# File 'lib/much-rails/service_validation_errors.rb', line 8

def initialize
  @hash = {}
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



6
7
8
# File 'lib/much-rails/service_validation_errors.rb', line 6

def hash
  @hash
end

Instance Method Details

#add(exception_class, &block) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/much-rails/service_validation_errors.rb', line 12

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

  @hash[exception_class] = block
end

#exception_classesObject



20
21
22
# File 'lib/much-rails/service_validation_errors.rb', line 20

def exception_classes
  @hash.keys
end

#result_for(ex) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/much-rails/service_validation_errors.rb', line 24

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