class CouchRest::Validation::GenericValidator

All validators extend this base class. Validators must:

@author Guy van den Berg

Attributes

field_name[R]
if_clause[RW]
unless_clause[RW]

Public Instance Methods

==(other) click to toggle source
# File lib/couchrest/validation/validators/generic_validator.rb, line 108
def ==(other)
  self.class == other.class &&
  self.field_name == other.field_name &&
  self.class == other.class &&
  self.if_clause == other.if_clause &&
  self.unless_clause == other.unless_clause &&
  self.instance_variable_get(:@options) == other.instance_variable_get(:@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

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

# File lib/couchrest/validation/validators/generic_validator.rb, line 68
def add_error(target, message, field_name = :general)
  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/couchrest/validation/validators/generic_validator.rb, line 78
def call(target)
  raise NotImplementedError, "CouchRest::Validation::GenericValidator::call must be overriden in a subclass"
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

# File lib/couchrest/validation/validators/generic_validator.rb, line 88
def execute?(target)
  if unless_clause = self.unless_clause
    if unless_clause.is_a?(Symbol)
      return false if target.send(unless_clause)
    elsif unless_clause.respond_to?(:call)
      return false if unless_clause.call(target)
    end
  end

  if if_clause = self.if_clause
    if if_clause.is_a?(Symbol)
      return target.send(if_clause)
    elsif if_clause.respond_to?(:call)
      return if_clause.call(target)
    end
  end

  true
end

Public Class Methods

new(field, opts = {}) click to toggle source

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

# File lib/couchrest/validation/validators/generic_validator.rb, line 53
def initialize(field, opts = {})
  @if_clause     = opts.delete(:if)
  @unless_clause = opts.delete(:unless)
end