Class: DataMapper::Validations::GenericValidator

### Home Classes Methods

### In Files

- lib/dm-validations/validators/generic_validator.rb

### Parent

Object

### Methods

- ::new
- #==
- #add_error
- #call
- #evaluate_conditional_clause
- #execute?
- #inspect
- #optional?
- #set_optional_by_default
- #to_s

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- DataMapper
- DataMapper::InvalidContextError
- DataMapper::Validate
- DataMapper::ValidationError
- DataMapper::Validations
- DataMapper::Validations::AbsenceValidator
- DataMapper::Validations::AcceptanceValidator
- DataMapper::Validations::AutoValidations
- DataMapper::Validations::AutoValidations::ModelExtension
- DataMapper::Validations::ClassMethods
- DataMapper::Validations::ConfirmationValidator
- DataMapper::Validations::Context
- DataMapper::Validations::ContextualValidators
- DataMapper::Validations::Format
- DataMapper::Validations::Format::Email
- DataMapper::Validations::Format::Url
- DataMapper::Validations::FormatValidator
- DataMapper::Validations::GenericValidator
- DataMapper::Validations::LengthValidator
- DataMapper::Validations::MethodValidator
- DataMapper::Validations::NumericalityValidator
- DataMapper::Validations::OrderedHash
- DataMapper::Validations::PresenceValidator
- DataMapper::Validations::PrimitiveTypeValidator
- DataMapper::Validations::UniquenessValidator
- DataMapper::Validations::UnknownValidationFormat
- DataMapper::Validations::ValidatesAbsence
- DataMapper::Validations::ValidatesAcceptance
- DataMapper::Validations::ValidatesConfirmation
- DataMapper::Validations::ValidatesFormat
- DataMapper::Validations::ValidatesLength
- DataMapper::Validations::ValidatesNumericality
- DataMapper::Validations::ValidatesPresence
- DataMapper::Validations::ValidatesPrimitiveType
- DataMapper::Validations::ValidatesUniqueness
- DataMapper::Validations::ValidatesWithBlock
- DataMapper::Validations::ValidatesWithMethod
- DataMapper::Validations::ValidatesWithin
- DataMapper::Validations::ValidationErrors
- DataMapper::Validations::WithinValidator
- Object

No matching classes.

# DataMapper::Validations::GenericValidator

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

### Attributes

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

### Public Class Methods

new(field_name, options = {}) click to toggle source

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

    # File lib/dm-validations/validators/generic_validator.rb, line 36
    def initialize(field_name, options = {})
      @field_name           = field_name
      @options              = DataMapper::Ext::Hash.except(options, :if, :unless)
      @if_clause            = options[:if]
      @unless_clause        = options[:unless]
      @humanized_field_name = DataMapper::Inflector.humanize(@field_name)
    end

### Public Instance Methods

==(other) click to toggle source

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

    # File lib/dm-validations/validators/generic_validator.rb, line 152
    def ==(other)
      self.class == other.class &&
      self.field_name == other.field_name &&
      self.if_clause == other.if_clause &&
      self.unless_clause == other.unless_clause &&
      self.options == other.options
    end

add_error(target, message, field_name = :general) click to toggle source

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.

    # File lib/dm-validations/validators/generic_validator.rb, line 57
    def add_error(target, message, field_name = :general)
      # TODO: should the field_name for a general message be :default???
      target.errors.add(field_name, message)
    end

call(target) click to toggle source

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.

    # File lib/dm-validations/validators/generic_validator.rb, line 72
    def call(target)
      raise NotImplementedError, "#{self.class}#call must be implemented"
    end

evaluate_conditional_clause(target, clause) click to toggle source

@api private

    # File lib/dm-validations/validators/generic_validator.rb, line 98
    def evaluate_conditional_clause(target, clause)
      if clause.kind_of?(Symbol)
        target.__send__(clause)
      elsif clause.respond_to?(:call)
        clause.call(target)
      end
    end

execute?(target) click to toggle source

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

    # File lib/dm-validations/validators/generic_validator.rb, line 87
    def execute?(target)
      if unless_clause = self.unless_clause
        !evaluate_conditional_clause(target, unless_clause)
      elsif if_clause = self.if_clause
        evaluate_conditional_clause(target, if_clause)
      else
        true
      end
    end

inspect() click to toggle source

    # File lib/dm-validations/validators/generic_validator.rb, line 160
    def inspect
      "<##{self.class.name} @field_name='#{@field_name}' @if_clause=#{@if_clause.inspect} @unless_clause=#{@unless_clause.inspect} @options=#{@options.inspect}>"
    end

Also aliased as: to_s
optional?(value) click to toggle source

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

    # File lib/dm-validations/validators/generic_validator.rb, line 128
    def optional?(value)
      if value.nil?
        @options[:allow_nil] ||
          (@options[:allow_blank] && !@options.has_key?(:allow_nil))
      elsif DataMapper::Ext.blank?(value)
        @options[:allow_blank]
      end
    end

set_optional_by_default(default = true) click to toggle source

Set the default value for allow_nil and allow_blank

@param [Boolean] default value

@api private

    # File lib/dm-validations/validators/generic_validator.rb, line 111
    def set_optional_by_default(default = true)
      [ :allow_nil, :allow_blank ].each do |key|
        @options[key] = true unless options.key?(key)
      end
    end

to_s() click to toggle source
Alias for: inspect

[Validate]

Generated with the Darkfish Rdoc Generator 2.