Class: Aux::Validations::Errors
- Inherits:
-
Object
- Object
- Aux::Validations::Errors
- Defined in:
- lib/aux/validations/errors.rb
Overview
Describes a collection of errors that can be initialized with an attribute name or an object to be validated
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
- #scope ⇒ String? readonly
Instance Method Summary collapse
- #add(attribute, subject, **details) ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean
- #group_by_attribute ⇒ Hash<Symbol, Array>
-
#initialize(subject, errors = []) ⇒ Errors
constructor
A new instance of Errors.
- #map(&block) ⇒ Object
Constructor Details
#initialize(subject) ⇒ Errors #initialize(attribute, errors) ⇒ Errors
Returns a new instance of Errors.
18 19 20 21 22 |
# File 'lib/aux/validations/errors.rb', line 18 def initialize(subject, errors = []) @scope = subject.is_a?(Symbol) ? nil : subject.class.name.underscore.tr('/', '.') @attribute = subject.is_a?(Symbol) ? subject : nil @errors = errors end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
11 |
# File 'lib/aux/validations/errors.rb', line 11 attr_reader :scope, :attribute |
#scope ⇒ String? (readonly)
11 12 13 |
# File 'lib/aux/validations/errors.rb', line 11 def scope @scope end |
Instance Method Details
#add(attribute, type, **details) ⇒ Object #add(attribute, errors) ⇒ Object #add(attribute, errors) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/aux/validations/errors.rb', line 37 def add(attribute, subject, **details) case subject when Symbol add_error(attribute, subject, **details) when Array add_nested_error(attribute, subject) when Errors add_nested_error(attribute, [subject]) else raise(ArgumentError, "Unsupported argument given (#{subject.class})") end end |
#clear ⇒ Object
50 51 52 |
# File 'lib/aux/validations/errors.rb', line 50 def clear self.errors = [] end |
#empty? ⇒ Boolean
55 56 57 |
# File 'lib/aux/validations/errors.rb', line 55 def empty? errors.empty? end |
#group_by_attribute ⇒ Hash<Symbol, Array>
65 66 67 |
# File 'lib/aux/validations/errors.rb', line 65 def group_by_attribute errors.group_by(&:attribute) end |
#map(&block) ⇒ Object
60 61 62 |
# File 'lib/aux/validations/errors.rb', line 60 def map(&block) errors.map(&block) end |