Class: Sequel::Model::Validation::Errors

Inherits:
Hash show all
Defined in:
lib/sequel_model/validations.rb

Overview

Validation::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



13
14
15
# File 'lib/sequel_model/validations.rb', line 13

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

Instance Method Details

#add(att, msg) ⇒ Object

Adds an error for the given attribute.



18
19
20
# File 'lib/sequel_model/validations.rb', line 18

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

#countObject

Return the total number of error messages.



23
24
25
# File 'lib/sequel_model/validations.rb', line 23

def count
  full_messages.length
end

#full_messagesObject

Returns an array of fully-formatted error messages.



28
29
30
31
32
33
34
# File 'lib/sequel_model/validations.rb', line 28

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.



38
39
40
# File 'lib/sequel_model/validations.rb', line 38

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