Sha256: a6ddffcedf7e30b5cff0ea54e155398268f8cddf7b003270ef3d0c9cebcff1b8

Contents?: true

Size: 1.84 KB

Versions: 16

Compression:

Stored size: 1.84 KB

Contents

module MongoMapper
  module Validations    
    module Macros
      def validates_uniqueness_of(*args)
        add_validations(args, MongoMapper::Validations::ValidatesUniquenessOf)
      end

      def validates_exclusion_of(*args)
        add_validations(args, MongoMapper::Validations::ValidatesExclusionOf)
      end

      def validates_inclusion_of(*args)
        add_validations(args, MongoMapper::Validations::ValidatesInclusionOf)
      end
    end
    
    class ValidatesUniquenessOf < Validatable::ValidationBase
      option :scope
      
      def valid?(instance)
        doc = instance.class.find(:first, :conditions => {self.attribute => instance[attribute]}.merge(scope_conditions(instance)), :limit => 1)
        doc.nil? || instance.id == doc.id
      end

      def message(instance)
        super || "has already been taken"
      end
      
      def scope_conditions(instance)
        return {} unless scope
        Array(scope).inject({}) do |conditions, key|
          conditions.merge(key => instance[key])
        end
      end
    end
    
    class ValidatesExclusionOf < Validatable::ValidationBase
      required_option :within
      
      def valid?(instance)
        value = instance[attribute]
        return true if allow_nil && value.nil?
        return true if allow_blank && value.blank?
        
        !within.include?(instance[attribute])
      end
      
      def message(instance)
        super || "is reserved"
      end
    end

    class ValidatesInclusionOf < Validatable::ValidationBase
      required_option :within
      
      def valid?(instance)
        value = instance[attribute]
        return true if allow_nil && value.nil?
        return true if allow_blank && value.blank?
        
        within.include?(value)
      end
      
      def message(instance)
        super || "is not in the list"
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
mongo_mapper-0.5.6 lib/mongo_mapper/validations.rb
djsun-mongo_mapper-0.5.5.3 lib/mongo_mapper/validations.rb
mongo_mapper-unstable-2009.10.16 lib/mongo_mapper/validations.rb
djsun-mongo_mapper-0.5.5.2 lib/mongo_mapper/validations.rb
mongo_mapper-0.5.5 lib/mongo_mapper/validations.rb
mongo_mapper-unstable-2009.10.12 lib/mongo_mapper/validations.rb
djsun-mongo_mapper-0.5.4.1 lib/mongo_mapper/validations.rb
djsun-mongo_mapper-0.5.3.1 lib/mongo_mapper/validations.rb
mongo_mapper-0.5.4 lib/mongo_mapper/validations.rb
mongo_mapper-0.5.3 lib/mongo_mapper/validations.rb
mongo_mapper-unstable-2009.10.11 lib/mongo_mapper/validations.rb
djsun-mongo_mapper-0.5.2.1 lib/mongo_mapper/validations.rb
mongo_mapper-0.5.2 lib/mongo_mapper/validations.rb
djsun-mongo_mapper-0.5.0.1 lib/mongo_mapper/validations.rb
mongo_mapper-0.5.1 lib/mongo_mapper/validations.rb
mongo_mapper-0.5.0 lib/mongo_mapper/validations.rb