Class: DataMapper::ValidationErrors

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

Instance Method Summary collapse

Constructor Details

#initializeValidationErrors

Returns a new instance of ValidationErrors.



5
6
7
# File 'lib/data_mapper/validations/validation_errors.rb', line 5

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

Instance Method Details

#add(attribute, message) ⇒ Object

Add a validation error. Use the attribute :general if the error doesn’t apply to a specific attribute.



16
17
18
# File 'lib/data_mapper/validations/validation_errors.rb', line 16

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

#clear!Object

Clear existing validation errors.



10
11
12
# File 'lib/data_mapper/validations/validation_errors.rb', line 10

def clear!
  @errors.clear
end

#empty?Boolean

Are any errors present?

Returns:

  • (Boolean)


28
29
30
# File 'lib/data_mapper/validations/validation_errors.rb', line 28

def empty?
  @errors.empty?
end

#full_messagesObject

Collect all errors into a single list.



21
22
23
24
25
# File 'lib/data_mapper/validations/validation_errors.rb', line 21

def full_messages
  @errors.inject([]) do |list,pair|
    list += pair.last
  end
end