Class: Lotus::Validations::Errors
- Inherits:
-
Object
- Object
- Lotus::Validations::Errors
- Defined in:
- lib/lotus/validations/errors.rb
Overview
A set of errors for a validator
This is the result of calling ‘#valid?` on a validator.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
(also: #eql?)
Check if the current set of errors equals to the one who belongs to ‘other`.
-
#add(attribute, *errors) ⇒ Object
private
Add an error to the set.
-
#any? ⇒ TrueClass, FalseClass
Check if the set has any entry.
-
#clear ⇒ Object
private
Clears the internal state of the errors.
-
#count ⇒ Fixnum
(also: #size)
Returns how many validations have failed.
-
#each(&blk) {|error| ... } ⇒ Object
Iterate thru the errors and yields the given block.
-
#empty? ⇒ TrueClass, FalseClass
Check if the set is empty.
-
#for(attribute) ⇒ Object
Return the errors for the given attribute.
-
#initialize ⇒ Errors
constructor
private
Initialize the errors.
-
#map(&blk) {|error| ... } ⇒ Object
Iterate thru the errors, yields the given block and collect the returning value.
-
#to_a ⇒ Array
Return a flat collection of errors.
-
#to_h ⇒ Lotus::Utils::Hash
Return a serializable Hash representation of the errors.
Constructor Details
#initialize ⇒ Errors
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize the errors
17 18 19 |
# File 'lib/lotus/validations/errors.rb', line 17 def initialize @errors = Hash.new end |
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass Also known as: eql?
Check if the current set of errors equals to the one who belongs to ‘other`.
120 121 122 123 |
# File 'lib/lotus/validations/errors.rb', line 120 def ==(other) other.is_a?(self.class) && other.errors == errors end |
#add(attribute, *errors) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add an error to the set
96 97 98 99 100 101 |
# File 'lib/lotus/validations/errors.rb', line 96 def add(attribute, *errors) if errors.any? @errors[attribute] ||= [] @errors[attribute].push(*errors) end end |
#any? ⇒ TrueClass, FalseClass
Check if the set has any entry
39 40 41 |
# File 'lib/lotus/validations/errors.rb', line 39 def any? @errors.any? end |
#clear ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Clears the internal state of the errors
58 59 60 |
# File 'lib/lotus/validations/errors.rb', line 58 def clear @errors.clear end |
#count ⇒ Fixnum Also known as: size
Returns how many validations have failed
48 49 50 |
# File 'lib/lotus/validations/errors.rb', line 48 def count errors.count end |
#each(&blk) {|error| ... } ⇒ Object
Iterate thru the errors and yields the given block
70 71 72 |
# File 'lib/lotus/validations/errors.rb', line 70 def each(&blk) errors.each(&blk) end |
#empty? ⇒ TrueClass, FalseClass
Check if the set is empty
28 29 30 |
# File 'lib/lotus/validations/errors.rb', line 28 def empty? @errors.empty? end |
#for(attribute) ⇒ Object
Return the errors for the given attribute
108 109 110 |
# File 'lib/lotus/validations/errors.rb', line 108 def for(attribute) @errors.fetch(attribute) { [] } end |
#map(&blk) {|error| ... } ⇒ Object
Iterate thru the errors, yields the given block and collect the returning value.
83 84 85 |
# File 'lib/lotus/validations/errors.rb', line 83 def map(&blk) errors.map(&blk) end |
#to_a ⇒ Array
Return a flat collection of errors.
141 142 143 |
# File 'lib/lotus/validations/errors.rb', line 141 def to_a errors.dup end |
#to_h ⇒ Lotus::Utils::Hash
Return a serializable Hash representation of the errors.
132 133 134 |
# File 'lib/lotus/validations/errors.rb', line 132 def to_h Utils::Hash.new(@errors).deep_dup end |