Class: Validation::Errors

Inherits:
Object show all
Defined in:
lib/assistance/validation.rb

Overview

Validation::Errors represents validation errors.

Instance Method Summary collapse

Constructor Details

#initializeErrors

Initializes a new instance of validation errors.



42
43
44
# File 'lib/assistance/validation.rb', line 42

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

Instance Method Details

#add(att, msg) ⇒ Object

Adds an error for the given attribute.



63
64
65
# File 'lib/assistance/validation.rb', line 63

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

#clearObject

Clears all errors.



52
53
54
# File 'lib/assistance/validation.rb', line 52

def clear
  @errors.clear
end

#empty?Boolean

Returns true if no errors are stored.

Returns:

  • (Boolean)


47
48
49
# File 'lib/assistance/validation.rb', line 47

def empty?
  @errors.empty?
end

#full_messagesObject

Returns an array of fully-formatted error messages.



68
69
70
71
72
73
# File 'lib/assistance/validation.rb', line 68

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.



57
58
59
# File 'lib/assistance/validation.rb', line 57

def on(att)
  @errors[att]
end