lib/mongoid/relations/targets/enumerable.rb in mongoid-4.0.2 vs lib/mongoid/relations/targets/enumerable.rb in mongoid-5.0.0.beta
- old
+ new
@@ -43,11 +43,11 @@
#
# @return [ true, false ] If the objects are equal in a case.
#
# @since 3.1.4
def ===(other)
- other.class == Class ? Array == other : self == other
+ other.class == Class ? (Array == other || Enumerable == other) : self == other
end
# Append a document to the enumerable.
#
# @example Append the document.
@@ -173,11 +173,12 @@
unless block_given?
return to_enum
end
if _loaded?
_loaded.each_pair do |id, doc|
- yield(doc)
+ document = _added.delete(doc._id) || doc
+ yield(document)
end
else
unloaded_documents.each do |doc|
document = _added.delete(doc._id) || _loaded.delete(doc._id) || doc
_loaded[document._id] = document
@@ -215,11 +216,14 @@
#
# @return [ Document ] The first document found.
#
# @since 2.1.0
def first
- matching_document(:first)
+ _loaded.try(:values).try(:first) ||
+ _added[(ul = _unloaded.try(:first)).try(:id)] ||
+ ul ||
+ _added.values.try(:first)
end
# Initialize the new enumerable either with a criteria or an array.
#
# @example Initialize the enumerable with a criteria.
@@ -296,11 +300,14 @@
#
# @return [ Document ] The last document found.
#
# @since 2.1.0
def last
- matching_document(:last)
+ _added.values.try(:last) ||
+ _loaded.try(:values).try(:last) ||
+ _added[(ul = _unloaded.try(:last)).try(:id)] ||
+ ul
end
# Loads all the documents in the enumerable from the database.
#
# @example Load all the documents.
@@ -453,16 +460,9 @@
private
def method_missing(name, *args, &block)
entries.send(name, *args, &block)
- end
-
- def matching_document(location)
- _loaded.try(:values).try(location) ||
- _added[(ul = _unloaded.try(location)).try(:id)] ||
- ul ||
- _added.values.try(location)
end
def unloaded_documents
_unloaded.selector.values.any?(&:blank_criteria?) ? [] : _unloaded
end