class Presto::Errors

Constants

ATTRIBUTE_JOINER

Public Instance Methods

add(att, msg) click to toggle source

Adds an error for the given attribute.

errors.add(:name, 'is not valid') if name == 'invalid'
# File lib/presto/errors.rb, line 8
def add(att, msg)
        fetch(att){self[att] = []} << msg
end
count() click to toggle source

Return the total number of error messages.

errors.count # => 3
# File lib/presto/errors.rb, line 15
def count
        values.inject(0){|m, v| m + v.length}
end
empty?() click to toggle source

Return true if there are no error messages, false otherwise.

# File lib/presto/errors.rb, line 20
def empty?
        count == 0
end
full_messages() click to toggle source

Returns an array of fully-formatted error messages.

errors.full_messages
# => ['name is not valid',
#     'hometown is not at least 2 letters']
# File lib/presto/errors.rb, line 29
def full_messages
        inject([]) do |m, kv| 
                att, errors = *kv
                errors.each {|e| m << "#{att} #{e}"}
                m
        end
end
on(att) click to toggle source

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
# File lib/presto/errors.rb, line 42
def on(att)
        if v = fetch(att, nil) and !v.empty?
                v
        end
end