Class: DataMapper::Validate::ValidationErrors

  • Object
    • DataMapper::Validate::ValidationErrors

Constructor Summary

public initialize
[View source]


10
11
12
13
14
15
# File 'dm-more/dm-validations/lib/dm-validations/validation_errors.rb', line 10

def initialize



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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

public method_missing(meth, *args, &block)
[View source]


48
49
50
# File 'dm-more/dm-validations/lib/dm-validations/validation_errors.rb', line 48

def method_missing(meth, *args, &block)
  @errors.send(meth, *args, &block)
end

Public Visibility

Public Instance Method Summary

#add(field_name, message)

Add a validation error.

#clear!

Clear existing validation errors.

#each
#full_messages

Collect all errors into a single list.

#on(field_name)

Return validation errors for a particular field_name.

Public Instance Methods Inherited from Object

validatable?

Public Instance Method Details

add

public add(field_name, message)

Add a validation error. Use the field_name :general if the errors does not apply to a specific field of the Resource.

Meta Tags

Parameters:

<Symbol>

field_name the name of the field that caused the error

<String>

message the message to add

[View source]


24
25
26
# File 'dm-more/dm-validations/lib/dm-validations/validation_errors.rb', line 24

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

clear!

public clear!

Clear existing validation errors.

[View source]


15
16
17
18
19
20
# File 'dm-more/dm-validations/lib/dm-validations/validation_errors.rb', line 15

def clear!



  @errors.clear
end

each

public each
[View source]


42
43
44
45
46
47
48
49
# File 'dm-more/dm-validations/lib/dm-validations/validation_errors.rb', line 42

def each



  @errors.map.each do |k,v|
    yield(v)
  end
end

full_messages

public full_messages

Collect all errors into a single list.

[View source]


29
30
31
32
33
34
35
36
# File 'dm-more/dm-validations/lib/dm-validations/validation_errors.rb', line 29

def full_messages



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

on

public on(field_name)

Return validation errors for a particular field_name.

Meta Tags

Parameters:

<Symbol>

field_name the name of the field you want an error for

[View source]


38
39
40
# File 'dm-more/dm-validations/lib/dm-validations/validation_errors.rb', line 38

def on(field_name)
  @errors[field_name].empty? ? nil : @errors[field_name]
end