Sha256: 597296a2b83b34b4cafb62d19124c2ba78886e196f304f4cf10b8716c12aac95

Contents?: true

Size: 1.2 KB

Versions: 26

Compression:

Stored size: 1.2 KB

Contents

module MongoMapper
  module Validations
    class ValidatesUniquenessOf < Validatable::ValidationBase
      def valid?(instance)
        # TODO: scope
        doc = instance.class.find(:first, :conditions => {self.attribute => instance[attribute]}, :limit => 1)
        doc.nil? || instance.id == doc.id
      end

      def message(instance)
        super || "has already been taken"
      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

26 entries across 26 versions & 6 rubygems

Version Path
jnunemaker-mongomapper-0.3.2 lib/mongomapper/validations.rb
jnunemaker-mongomapper-0.3.3 lib/mongomapper/validations.rb
jnunemaker-mongomapper-0.3.4 lib/mongomapper/validations.rb
jnunemaker-mongomapper-0.3.5 lib/mongomapper/validations.rb
nirvdrum-mongomapper-0.3.3 lib/mongomapper/validations.rb
shingara-mongomapper-0.3.3 lib/mongomapper/validations.rb