Class: Sequel::Model::Errors

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

Overview

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

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



7
8
9
# File 'lib/sequel/model/errors.rb', line 7

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

Instance Method Details

#add(att, msg) ⇒ Object

Adds an error for the given attribute.



12
13
14
# File 'lib/sequel/model/errors.rb', line 12

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

#countObject

Return the total number of error messages.



17
18
19
# File 'lib/sequel/model/errors.rb', line 17

def count
  full_messages.length
end

#full_messagesObject

Returns an array of fully-formatted error messages.



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

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



32
33
34
# File 'lib/sequel/model/errors.rb', line 32

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