Sha256: a6f7796ff77427d1468520d1374d4021be67cf166686a43026c9150301a71662

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

dirname = File.dirname(__FILE__)

require dirname + '/validation_errors'
require dirname + '/contextual_validations'
require dirname + '/generic_validator'

Dir[dirname + '/*_validator.rb'].reject do |path|
  path =~ /\/generic_validator/
end.each do |validator|
  load validator
end

module DataMapper
  module Extensions
    
    module ValidationHelper
      
      def self.included(base)
        base.extend(ClassMethods)
        base.class_eval do
          include DataMapper::Validations::ValidatesPresenceOf
          include DataMapper::Validations::ValidatesLengthOf
          include DataMapper::Validations::ValidatesConfirmationOf
          include DataMapper::Validations::ValidatesUniquenessOf
          include DataMapper::Validations::ValidatesFormatOf
        end
      end
      
      def errors
        @errors ||= ValidationErrors.new
      end
      
      def valid?(context = :general)
        self.class.validations.execute(context, self)
      end
      
      module ClassMethods
        
        def validations
          @validations ||= ContextualValidations.new
        end
        
        def retrieve_options_from_arguments_for_validators(args, defaults = nil)
          options = args.last.kind_of?(Hash) ? args.pop : {}
          
          context = :general
          context = options[:context] if options.has_key?(:context)
          context = options.delete(:on) if options.has_key?(:on)
          options[:context] = context
          
          options.merge!(defaults) unless defaults.nil?
          return options
        end
                
      end
      
    end    
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datamapper-0.1.0 lib/data_mapper/validations/validation_helper.rb