Class: Presto::Errors
- Inherits:
-
Hash
- Object
- Hash
- Presto::Errors
- Defined in:
- lib/presto/errors.rb
Constant Summary collapse
- ATTRIBUTE_JOINER =
' and '.freeze
Instance Method Summary collapse
-
#add(att, msg) ⇒ Object
Adds an error for the given attribute.
-
#count ⇒ Object
Return the total number of error messages.
-
#empty? ⇒ Boolean
Return true if there are no error messages, false otherwise.
-
#full_messages ⇒ Object
Returns an array of fully-formatted error messages.
-
#on(att) ⇒ Object
Returns the array of errors for the given attribute, or nil if there are no errors for the attribute.
Instance Method Details
#add(att, msg) ⇒ Object
Adds an error for the given attribute.
errors.add(:name, 'is not valid') if name == 'invalid'
8 9 10 |
# File 'lib/presto/errors.rb', line 8 def add(att, msg) fetch(att){self[att] = []} << msg end |
#count ⇒ Object
Return the total number of error messages.
errors.count # => 3
15 16 17 |
# File 'lib/presto/errors.rb', line 15 def count values.inject(0){|m, v| m + v.length} end |
#empty? ⇒ Boolean
Return true if there are no error messages, false otherwise.
20 21 22 |
# File 'lib/presto/errors.rb', line 20 def empty? count == 0 end |
#full_messages ⇒ Object
Returns an array of fully-formatted error messages.
errors.
# => ['name is not valid',
# 'hometown is not at least 2 letters']
29 30 31 32 33 34 35 |
# File 'lib/presto/errors.rb', line 29 def inject([]) do |m, kv| att, errors = *kv errors.each {|e| m << "#{att} #{e}"} m end end |
#on(att) ⇒ Object
Returns the array of errors for the given attribute, or nil if there are no errors for the attribute.
errors.on(:name) # => ['name is not valid']
errors.on(:id) # => nil
42 43 44 45 46 |
# File 'lib/presto/errors.rb', line 42 def on(att) if v = fetch(att, nil) and !v.empty? v end end |