lib/dm-validations/generic_validator.rb in dm-validations-0.9.9 vs lib/dm-validations/generic_validator.rb in dm-validations-0.9.10

- old
+ new

@@ -1,5 +1,6 @@ +# -*- coding: utf-8 -*- module DataMapper module Validate # All validators extend this base class. Validators must: # @@ -11,12 +12,12 @@ # # @author Guy van den Berg # @since 0.9 class GenericValidator - attr_accessor :if_clause - attr_accessor :unless_clause + attr_accessor :if_clause, :unless_clause + attr_reader :field_name # Construct a validator. Capture the :if and :unless clauses when present. # # @param field<String, Symbol> The property specified for validation # @@ -26,12 +27,12 @@ # determine if the validation should not occur # All additional key/value pairs are passed through to the validator # that is sub-classing this GenericValidator # def initialize(field, opts = {}) - @if_clause = opts.has_key?(:if) ? opts[:if] : nil - @unless_clause = opts.has_key?(:unless) ? opts[:unless] : nil + @if_clause = opts.delete(:if) + @unless_clause = opts.delete(:unless) end # 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. @@ -41,24 +42,20 @@ # @param <Symbol> field_name the name of the field that caused the error # # TODO - should the field_name for a general message be :default??? # def add_error(target, message, field_name = :general) - target.errors.add(field_name,message) + target.errors.add(field_name, message) end # 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 def call(target) - raise "DataMapper::Validate::GenericValidator::call must be overriden in #{self.class.to_s}" - end - - def field_name - @field_name + raise NotImplementedError, "DataMapper::Validate::GenericValidator::call must be overriden in a subclass" end # Determines if this validator should be run against the # target by evaluating the :if and :unless clauses # optionally passed while specifying any validator.