Errors represents validation errors, a simple hash subclass with a few convenience methods.
Assign an array of messages for each attribute on access. Using this message is discouraged in new code, use add to add new error messages, and on to check existing error messages.
# File lib/sequel/model/errors.rb, line 12 12: def [](k) 13: has_key?(k) ? super : (self[k] = []) 14: end
Adds an error for the given attribute.
errors.add(:name, 'is not valid') if name == 'invalid'
# File lib/sequel/model/errors.rb, line 19 19: def add(att, msg) 20: self[att] << msg 21: end
Return the total number of error messages.
errors.count # => 3
# File lib/sequel/model/errors.rb, line 26 26: def count 27: values.inject(0){|m, v| m + v.length} 28: end
Return true if there are no error messages, false otherwise.
# File lib/sequel/model/errors.rb, line 31 31: def empty? 32: count == 0 33: end
Returns an array of fully-formatted error messages.
errors.full_messages # => ['name is not valid', # 'hometown is not at least 2 letters']
# File lib/sequel/model/errors.rb, line 40 40: def full_messages 41: inject([]) do |m, kv| 42: att, errors = *kv 43: errors.each {|e| m << (e.is_a?(LiteralString) ? e : "#{Array(att).join(ATTRIBUTE_JOINER)} #{e}")} 44: m 45: end 46: end
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/sequel/model/errors.rb, line 53 53: def on(att) 54: if v = fetch(att, nil) and !v.empty? 55: v 56: end 57: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.