Class: Validus::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/validus/errors.rb

Overview

Represents a collection of errors.

Instance Method Summary collapse

Constructor Details

#initializeErrors

Returns a new instance of Errors.



4
5
6
# File 'lib/validus/errors.rb', line 4

def initialize
  @errors = Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#add(attribute, message) ⇒ Object

Add an error message for an attribute.



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

def add(attribute, message)
  @errors[attribute] << message
end

#clearObject



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

def clear
  @errors.clear
end

#empty?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/validus/errors.rb', line 13

def empty?
  @errors.empty?
end

#for(attribute) ⇒ Object

Returns an Enumerator consisting of all errors associated with attribute.



23
24
25
# File 'lib/validus/errors.rb', line 23

def for(attribute)
  @errors[attribute].to_enum
end

#full_messagesObject

Returns an Enumerator consisting of all errors for all attributes. The name of the attribute is pre-pended to the error message.



29
30
31
32
33
# File 'lib/validus/errors.rb', line 29

def full_messages
  @errors.map do |k, v|
    v.map { |error| "#{k} #{error}"}
  end.flatten.to_enum
end