Class: Validation::Errors

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

Overview

Validation::Errors represents validation errors.

Instance Method Summary collapse

Constructor Details

#initializeErrors

Initializes a new instance of validation errors.



45
46
47
# File 'lib/sequel_model/validations.rb', line 45

def initialize
  @errors = Hash.new {|h, k| h[k] = []}
end

Instance Method Details

#add(att, msg) ⇒ Object

Adds an error for the given attribute.



50
51
52
# File 'lib/sequel_model/validations.rb', line 50

def add(att, msg)
  @errors[att] << msg
end

#clearObject

Clears all errors.



55
56
57
# File 'lib/sequel_model/validations.rb', line 55

def clear
  @errors.clear
end

#each(&block) ⇒ Object

Iterates over errors



60
61
62
# File 'lib/sequel_model/validations.rb', line 60

def each(&block)
  @errors.each(&block)
end

#empty?Boolean

Returns true if no errors are stored.

Returns:

  • (Boolean)


65
66
67
# File 'lib/sequel_model/validations.rb', line 65

def empty?
  @errors.empty?
end

#full_messagesObject

Returns an array of fully-formatted error messages.



70
71
72
73
74
75
# File 'lib/sequel_model/validations.rb', line 70

def full_messages
  @errors.inject([]) do |m, kv| att, errors = *kv
    errors.each {|e| m << "#{att} #{e}"}
    m
  end
end

#on(att) ⇒ Object Also known as: []

Returns the errors for the given attribute.



78
79
80
# File 'lib/sequel_model/validations.rb', line 78

def on(att)
  @errors[att]
end

#sizeObject

Returns size of errors array



84
85
86
# File 'lib/sequel_model/validations.rb', line 84

def size
  @errors.size
end