Sha256: cee0e0b42b589ea223dbe414e265d4cc2d961755a8f16ed908bf983cc1025b41

Contents?: true

Size: 1.69 KB

Versions: 81

Compression:

Stored size: 1.69 KB

Contents

# encoding: utf-8
module Mongoid

  # This module contains the behaviour of Mongoid's clone/dup of documents.
  module Equality

    # Default comparison is via the string version of the id.
    #
    # @example Compare two documents.
    #   person <=> other_person
    #
    # @param [ Document ] other The document to compare with.
    #
    # @return [ Integer ] -1, 0, 1.
    #
    # @since 1.0.0
    def <=>(other)
      attributes["_id"].to_s <=> other.attributes["_id"].to_s
    end

    # Performs equality checking on the document ids. For more robust
    # equality checking please override this method.
    #
    # @example Compare for equality.
    #   document == other
    #
    # @param [ Document, Object ] other The other object to compare with.
    #
    # @return [ true, false ] True if the ids are equal, false if not.
    #
    # @since 1.0.0
    def ==(other)
      self.class == other.class &&
          attributes["_id"] == other.attributes["_id"]
    end

    # Performs class equality checking.
    #
    # @example Compare the classes.
    #   document === other
    #
    # @param [ Document, Object ] other The other object to compare with.
    #
    # @return [ true, false ] True if the classes are equal, false if not.
    #
    # @since 1.0.0
    def ===(other)
      other.class == Class ? self.class === other : self == other
    end

    # Delegates to ==. Used when needing checks in hashes.
    #
    # @example Perform equality checking.
    #   document.eql?(other)
    #
    # @param [ Document, Object ] other The object to check against.
    #
    # @return [ true, false ] True if equal, false if not.
    #
    # @since 1.0.0
    def eql?(other)
      self == (other)
    end
  end
end

Version data entries

81 entries across 76 versions & 6 rubygems

Version Path
mongoid-7.0.13 lib/mongoid/equality.rb
mongoid-7.0.12 lib/mongoid/equality.rb
mongoid-6.4.8 lib/mongoid/equality.rb
mongoid-7.0.11 lib/mongoid/equality.rb
mongoid-7.0.10 lib/mongoid/equality.rb
mongoid-6.4.7 lib/mongoid/equality.rb
mongoid-6.4.5 lib/mongoid/equality.rb
mongoid-7.0.8 lib/mongoid/equality.rb
mongoid-7.0.7 lib/mongoid/equality.rb
mongoid-7.0.6 lib/mongoid/equality.rb
mongoid-5.4.1 lib/mongoid/equality.rb
mongoid-7.0.5 lib/mongoid/equality.rb
mongoid-7.0.4 lib/mongoid/equality.rb
mongoid-6.4.4 lib/mongoid/equality.rb
mongoid-7.0.3 lib/mongoid/equality.rb
mongoid-6.4.2 lib/mongoid/equality.rb
mongoid-7.0.2 lib/mongoid/equality.rb
mongoid-7.0.1 lib/mongoid/equality.rb
mongoid-6.4.1 lib/mongoid/equality.rb
mongoid-5.4.0 lib/mongoid/equality.rb