Sha256: 05abc723acd62af0a78814994f451224f12198d1db18a88f49a3a9854269e46a

Contents?: true

Size: 1.72 KB

Versions: 27

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid

  # This module contains the behavior 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

27 entries across 27 versions & 2 rubygems

Version Path
mongoid-7.3.5 lib/mongoid/equality.rb
mongoid-7.3.4 lib/mongoid/equality.rb
mongoid-7.1.11 lib/mongoid/equality.rb
mongoid-7.2.6 lib/mongoid/equality.rb
mongoid-7.3.3 lib/mongoid/equality.rb
mongoid-7.3.2 lib/mongoid/equality.rb
mongoid-7.2.5 lib/mongoid/equality.rb
mongoid-7.1.10 lib/mongoid/equality.rb
mongoid-7.1.9 lib/mongoid/equality.rb
mongoid-7.2.4 lib/mongoid/equality.rb
mongoid-7.3.1 lib/mongoid/equality.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/lib/mongoid/equality.rb
mongoid-7.3.0 lib/mongoid/equality.rb
mongoid-7.2.3 lib/mongoid/equality.rb
mongoid-7.1.8 lib/mongoid/equality.rb
mongoid-7.2.2 lib/mongoid/equality.rb
mongoid-7.2.1 lib/mongoid/equality.rb
mongoid-7.1.7 lib/mongoid/equality.rb
mongoid-7.2.0 lib/mongoid/equality.rb
mongoid-7.1.6 lib/mongoid/equality.rb