Module: Mongoid::Equality

Extended by:
ActiveSupport::Concern
Included in:
Composable
Defined in:
build/mongoid-master/lib/mongoid/equality.rb

Overview

This module contains the behavior of Mongoid's clone/dup of documents.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Integer

Default comparison is via the string version of the id.

Examples:

Compare two documents.

person <=> other_person

Parameters:

  • other (Document)

    The document to compare with.

Returns:

  • (Integer)

    -1, 0, 1.



19
20
21
# File 'build/mongoid-master/lib/mongoid/equality.rb', line 19

def <=>(other)
  attributes["_id"].to_s <=> other.attributes["_id"].to_s
end

#==(other) ⇒ true | false

Performs equality checking on the document ids. For more robust equality checking please override this method.

Examples:

Compare for equality.

document == other

Parameters:

  • other (Document | Object)

    The other object to compare with.

Returns:

  • (true | false)

    True if the ids are equal, false if not.



32
33
34
35
# File 'build/mongoid-master/lib/mongoid/equality.rb', line 32

def ==(other)
  self.class == other.class &&
      attributes["_id"] == other.attributes["_id"]
end

#===(other) ⇒ true | false

Performs class equality checking.

Examples:

Compare the classes.

document === other

Parameters:

  • other (Document | Object)

    The other object to compare with.

Returns:

  • (true | false)

    True if the classes are equal, false if not.



45
46
47
48
49
50
51
# File 'build/mongoid-master/lib/mongoid/equality.rb', line 45

def ===(other)
  if Mongoid.legacy_triple_equals
    other.class == Class ? self.class === other : self == other
  else
    super
  end
end

#eql?(other) ⇒ true | false

Delegates to ==. Used when needing checks in hashes.

Examples:

Perform equality checking.

document.eql?(other)

Parameters:

  • other (Document | Object)

    The object to check against.

Returns:

  • (true | false)

    True if equal, false if not.



61
62
63
# File 'build/mongoid-master/lib/mongoid/equality.rb', line 61

def eql?(other)
  self == (other)
end