class Sequel::Model::Errors

  1. lib/sequel/model/errors.rb
Superclass: Hash

Errors represents validation errors, a simple hash subclass with a few convenience methods.

Methods

Public Instance

  1. add
  2. count
  3. empty?
  4. full_messages
  5. on

Public Instance methods

add(att, msg)

Adds an error for the given attribute.

errors.add(:name, 'is not valid') if name == 'invalid'
[show source]
   # File lib/sequel/model/errors.rb
11 def add(att, msg)
12   fetch(att){self[att] = []} << msg
13 end
count()

Return the total number of error messages.

errors.count # => 3
[show source]
   # File lib/sequel/model/errors.rb
18 def count
19   values.inject(0){|m, v| m + v.length}
20 end
empty?()

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

[show source]
   # File lib/sequel/model/errors.rb
23 def empty?
24   count == 0
25 end
full_messages()

Returns an array of fully-formatted error messages.

errors.full_messages
# => ['name is not valid',
#     'hometown is not at least 2 letters']

If the message is a Sequel::LiteralString, it will be used literally, without the column name:

errors.add(:name, Sequel.lit("Album name is not valid"))
errors.full_messages
# => ['Album name is not valid']
[show source]
   # File lib/sequel/model/errors.rb
38 def full_messages
39   inject([]) do |m, kv| 
40     att, errors = *kv
41     errors.each {|e| m << (e.is_a?(LiteralString) ? e : full_message(att, e))}
42     m
43   end
44 end
on(att)

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
[show source]
   # File lib/sequel/model/errors.rb
51 def on(att)
52   if v = fetch(att, nil) and !v.empty?
53     v
54   end
55 end