Class: Sequel::Model::Errors

Inherits:
Hash show all
Defined in:
lib/sequel/lib/sequel/model/errors.rb

Overview

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

Constant Summary collapse

ATTRIBUTE_JOINER =
' and '

Instance Method Summary collapse

Methods inherited from Hash

#&, #case, #sql_expr, #sql_negate, #sql_or, #|, #~

Constructor Details

#initializeErrors

Assign an array of messages for each attribute on access


9
10
11
# File 'lib/sequel/lib/sequel/model/errors.rb', line 9

def initialize
  super{|h,k| h[k] = []}
end

Instance Method Details

#add(att, msg) ⇒ Object

Adds an error for the given attribute.


14
15
16
# File 'lib/sequel/lib/sequel/model/errors.rb', line 14

def add(att, msg)
  self[att] << msg
end

#countObject

Return the total number of error messages.


19
20
21
# File 'lib/sequel/lib/sequel/model/errors.rb', line 19

def count
  values.inject(0){|m, v| m + v.length}
end

#full_messagesObject

Returns an array of fully-formatted error messages.


24
25
26
27
28
29
30
# File 'lib/sequel/lib/sequel/model/errors.rb', line 24

def full_messages
  inject([]) do |m, kv| 
    att, errors = *kv
    errors.each {|e| m << "#{Array(att).join(ATTRIBUTE_JOINER)} #{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.


34
35
36
# File 'lib/sequel/lib/sequel/model/errors.rb', line 34

def on(att)
  self[att] if include?(att)
end