Class: MuchRails::ServiceValidationErrors
- Inherits:
-
Object
- Object
- MuchRails::ServiceValidationErrors
- Defined in:
- lib/much-rails/service_validation_errors.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Instance Method Summary collapse
- #add(exception_class, &block) ⇒ Object
- #exception_classes ⇒ Object
-
#initialize ⇒ ServiceValidationErrors
constructor
A new instance of ServiceValidationErrors.
- #result_for(ex) ⇒ Object
Constructor Details
#initialize ⇒ ServiceValidationErrors
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
#hash ⇒ Object (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_classes ⇒ Object
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 |