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 field<String, Symbol> The property specified for validation

@option :if<Symbol, Proc> The name of a method or a Proc to call to

                    determine if the validation should occur.

@option :unless<Symbol, Proc> The name of a method or a Proc to call to

                        determine if the validation should not occur

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

TODO - should the field_name for a general message be :default???

Test if the value is blank and is allowed

@param <Object> value to test @return <Boolean> true if blank is allowed and value is blank

Test if the value is nil and is allowed

@param <Object> value to test @return <Boolean> true if nil is allowed and value is nil

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

Test the value to see if it is blank or nil, and if it is allowed

@param <Object> value to test @return <Boolean> true if blank/nil is allowed, and the value is blank/nil

Set the default value for allow_nil and allow_blank

@param [Boolean] default value @return <undefined>

to_s()

Alias for inspect

[Validate]