Class DataMapper::Validations::GenericValidator
In: lib/dm-validations/validators/generic_validator.rb
Parent: Object

All validators extend this base class. Validators must:

  • Implement the initialize method to capture its parameters, also calling super to have this parent class capture the optional, general :if and :unless parameters.
  • Implement the call method, returning true or false. The call method provides the validation logic.

@author Guy van den Berg @since 0.9

Methods

Attributes

field_name  [R] 
humanized_field_name  [R] 
if_clause  [RW] 
options  [R] 
unless_clause  [RW] 

Public Class methods

Construct a validator. Capture the :if and :unless clauses when present.

@param [String, Symbol] field

  The property specified for validation.

@option [Symbol, Proc] :if

  The name of a method or a Proc to call to determine if the
  validation should occur.

@option [Symbol, Proc] :unless

  The name of a method or a Proc to call to determine if the
  validation should not occur.

@note

  All additional key/value pairs are passed through to the validator
  that is sub-classing this GenericValidator

Public Instance methods

Returns true if validators are equal

Note that this intentionally do validate options equality

even though it is hard to imagine a situation when multiple validations will be used on the same field with the same conditions but different options, it happens to be the case every once in a while with inferred validations for strings/text and explicitly given validations with different option (usually as Range vs. max limit for inferred validation)

@api semipublic

Add an error message to a target resource. If the error corresponds to a specific field of the resource, add it to that field, otherwise add it as a :general message.

@param [Object] target

  The resource that has the error.

@param [String] message

  The message to add.

@param [Symbol] field_name

  The name of the field that caused the error.

Call the validator. "call" is used so the operation is BoundMethod and Block compatible. This must be implemented in all concrete classes.

@param [Object] target

  The resource that the validator must be called against.

@return [Boolean]

  true if valid, otherwise false.

Determines if this validator should be run against the target by evaluating the :if and :unless clauses optionally passed while specifying any validator.

@param [Object] target

  The resource that we check against.

@return [Boolean]

  true if should be run, otherwise false.

@api private

Test the value to see if it is blank or nil, and if it is allowed. Note that allowing blank without explicitly denying nil allows nil values, since nil.blank? is true.

@param [Object] value

  The value to test.

@return [Boolean]

  true if blank/nil is allowed, and the value is blank/nil.

@api private

Set the default value for allow_nil and allow_blank

@param [Boolean] default value

@api private

to_s()

Alias for inspect

[Validate]