Class: DataMapper::Validate::ValidationErrors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dm-validations/validation_errors.rb

Overview

Author:

  • Guy van den Berg

Since:

  • 0.9

Constant Summary collapse

@@default_error_messages =

Since:

  • 0.9

{
  :absent                   => '%s must be absent',
  :inclusion                => '%s must be one of %s',
  :invalid                  => '%s has an invalid format',
  :confirmation             => '%s does not match the confirmation',
  :accepted                 => '%s is not accepted',
  :nil                      => '%s must not be nil',
  :blank                    => '%s must not be blank',
  :length_between           => '%s must be between %s and %s characters long',
  :too_long                 => '%s must be at most %s characters long',
  :too_short                => '%s must be at least %s characters long',
  :wrong_length             => '%s must be %s characters long',
  :taken                    => '%s is already taken',
  :not_a_number             => '%s must be a number',
  :not_an_integer           => '%s must be an integer',
  :greater_than             => '%s must be greater than %s',
  :greater_than_or_equal_to => '%s must be greater than or equal to %s',
  :equal_to                 => '%s must be equal to %s',
  :not_equal_to             => '%s must not be equal to %s',
  :less_than                => '%s must be less than %s',
  :less_than_or_equal_to    => '%s must be less than or equal to %s',
  :value_between            => '%s must be between %s and %s',
  :primitive                => '%s must be of type %s'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ ValidationErrors

Returns a new instance of ValidationErrors.

Since:

  • 0.9



49
50
51
52
# File 'lib/dm-validations/validation_errors.rb', line 49

def initialize(resource)
  @resource = resource
  @errors   = Dictionary.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Since:

  • 0.9



108
109
110
# File 'lib/dm-validations/validation_errors.rb', line 108

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

Instance Attribute Details

#resourceObject (readonly)

Since:

  • 0.9



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

def resource
  @resource
end

Class Method Details

.default_error_message(key, field, *values) ⇒ Object

Since:

  • 0.9



42
43
44
45
# File 'lib/dm-validations/validation_errors.rb', line 42

def self.default_error_message(key, field, *values)
  field = Extlib::Inflection.humanize(field)
  @@default_error_messages[key] % [field, *values].flatten
end

Instance Method Details

#add(field_name, message) ⇒ Object

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

Parameters:

  • field_name (Symbol)

    the name of the field that caused the error

  • message (String)

    the message to add

Since:

  • 0.9



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dm-validations/validation_errors.rb', line 64

def add(field_name, message)
  # see 6abe8fff in extlib, but don't enforce
  # it unless Edge version is installed
  if message.respond_to?(:try_call)

    # DM resource
    message = if resource.respond_to?(:model) && resource.model.respond_to?(:properties)
      message.try_call(resource, resource.model.properties[field_name])
    else
      # pure Ruby object
      message.try_call(resource)
    end
  end
  (errors[field_name] ||= []) << message
end

#clear!Object

Clear existing validation errors.

Since:

  • 0.9



55
56
57
# File 'lib/dm-validations/validation_errors.rb', line 55

def clear!
  errors.clear
end

#eachObject

Since:

  • 0.9



97
98
99
100
101
102
# File 'lib/dm-validations/validation_errors.rb', line 97

def each
  errors.map.each do |k, v|
    next if v.blank?
    yield(v)
  end
end

#empty?Boolean

Returns:

  • (Boolean)

Since:

  • 0.9



104
105
106
# File 'lib/dm-validations/validation_errors.rb', line 104

def empty?
  @errors.empty?
end

#full_messagesObject

Collect all errors into a single list.

Since:

  • 0.9



81
82
83
84
85
# File 'lib/dm-validations/validation_errors.rb', line 81

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

#on(field_name) ⇒ Array<DataMapper::Validate::Error>

Return validation errors for a particular field_name.

Parameters:

  • field_name (Symbol)

    the name of the field you want an error for

Returns:

  • (Array<DataMapper::Validate::Error>)

    array of validation errors or empty array, if there are no errors on given field

Since:

  • 0.9



92
93
94
95
# File 'lib/dm-validations/validation_errors.rb', line 92

def on(field_name)
  errors_for_field = errors[field_name]
  errors_for_field.blank? ? nil : errors_for_field.uniq
end