lib/pupa/processor/persistence.rb in pupa-0.0.9 vs lib/pupa/processor/persistence.rb in pupa-0.0.10
- old
+ new
@@ -28,11 +28,11 @@
end
end
# Saves an object to MongoDB.
#
- # @return [String] the object's database ID
+ # @return [Array] whether the object was inserted and the object's database ID
# @raises [Pupa::Errors::TooManyMatches] if multiple documents would be updated
def save
selector = @object.fingerprint
query = collection.find(selector)
@@ -40,14 +40,14 @@
@object.run_callbacks(:save) do
case query.count
when 0
@object.run_callbacks(:create) do
collection.insert(@object.to_h(persist: true))
- @object._id.to_s
+ [true, @object._id.to_s]
end
when 1
query.update(@object.to_h(persist: true))
- query.first['_id'].to_s
+ [false, query.first['_id'].to_s]
else
raise Errors::TooManyMatches, "selector matches multiple documents during save: #{collection_name} #{MultiJson.dump(selector)}"
end
end
end