Class: Validus::Errors
- Inherits:
-
Object
- Object
- Validus::Errors
- Defined in:
- lib/validus/errors.rb
Overview
Represents a collection of errors.
Instance Method Summary collapse
-
#add(attribute, message) ⇒ Object
Add an error
message
for anattribute
. - #clear ⇒ Object
- #empty? ⇒ Boolean
-
#for(attribute) ⇒ Object
Returns an Enumerator consisting of all errors associated with
attribute
. -
#full_messages ⇒ Object
Returns an Enumerator consisting of all errors for all attributes.
-
#initialize ⇒ Errors
constructor
A new instance of Errors.
Constructor Details
#initialize ⇒ Errors
Returns a new instance of Errors.
4 5 6 |
# File 'lib/validus/errors.rb', line 4 def initialize @errors = Hash.new { |hash, key| hash[key] = [] } end |
Instance Method Details
#add(attribute, message) ⇒ Object
Add an error message
for an attribute
.
9 10 11 |
# File 'lib/validus/errors.rb', line 9 def add(attribute, ) @errors[attribute] << end |
#clear ⇒ Object
17 18 19 |
# File 'lib/validus/errors.rb', line 17 def clear @errors.clear end |
#empty? ⇒ Boolean
13 14 15 |
# File 'lib/validus/errors.rb', line 13 def empty? @errors.empty? end |
#for(attribute) ⇒ Object
Returns an Enumerator consisting of all errors associated with attribute
.
23 24 25 |
# File 'lib/validus/errors.rb', line 23 def for(attribute) @errors[attribute].to_enum end |
#full_messages ⇒ Object
Returns an Enumerator consisting of all errors for all attributes. The name of the attribute is pre-pended to the error message.
29 30 31 32 33 |
# File 'lib/validus/errors.rb', line 29 def @errors.map do |k, v| v.map { |error| "#{k} #{error}"} end.flatten.to_enum end |