Class: ErrorNormalizer::Normalizer
- Inherits:
-
Object
- Object
- ErrorNormalizer::Normalizer
- Defined in:
- lib/error_normalizer/normalizer.rb
Overview
Responsible for converting input to the array of normalized errors.
Constant Summary collapse
- UnsupportedInputType =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
- #add_error(error, path: nil, **options) ⇒ Object
-
#initialize(input, namespace: nil, **config) ⇒ Normalizer
constructor
A new instance of Normalizer.
- #normalize ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(input, namespace: nil, **config) ⇒ Normalizer
Returns a new instance of Normalizer.
25 26 27 28 29 30 |
# File 'lib/error_normalizer/normalizer.rb', line 25 def initialize(input, namespace: nil, **config) @input = input @namespace = namespace @errors = [] @config = config end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
23 24 25 |
# File 'lib/error_normalizer/normalizer.rb', line 23 def errors @errors end |
Instance Method Details
#add_error(error, path: nil, **options) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/error_normalizer/normalizer.rb', line 32 def add_error(error, path: nil, **) @errors << case error when Error error when Symbol, String parse_error(error, path, ) end end |
#normalize ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/error_normalizer/normalizer.rb', line 42 def normalize case @input when Hash normalize_hash(@input.dup) when ActiveModel::Errors normalize_hash(@input.to_hash) else raise "Don't know how to normalize errors" end self end |
#to_a ⇒ Object
55 56 57 |
# File 'lib/error_normalizer/normalizer.rb', line 55 def to_a @errors.map(&:to_hash) end |