Class | DataMapper::Validations::GenericValidator |
In: |
lib/dm-validations/validators/generic_validator.rb
|
Parent: | Object |
All validators extend this base class. Validators must:
@author Guy van den Berg @since 0.9
field_name | [R] | |
humanized_field_name | [R] | |
if_clause | [RW] | |
options | [R] | |
unless_clause | [RW] |
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
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
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