Persistence operations include this module to get basic functionality on initialization.
Instantiate the new persistence operation.
@example Create the operation.
Operation.new(document, { :safe => true }, { "field" => "value" })
@param [ Document ] document The document to persist. @param [ Hash ] options The persistence options.
@since 2.1.0
# File lib/mongoid/persistence/operations.rb, line 52 def initialize(document, options = {}) @document, @options = document, options end
Get the collection we should be persisting to.
@example Get the collection.
operation.collection
@return [ Collection ] The collection to persist to.
@since 2.1.0
# File lib/mongoid/persistence/operations.rb, line 25 def collection @collection ||= document._root.collection end
Get the atomic delete operations for embedded documents.
@example Get the atomic deletes.
operation.deletes
@return [ Hash ] The atomic delete selector.
@since 2.1.0
# File lib/mongoid/persistence/operations.rb, line 37 def deletes { document.atomic_delete_modifier => { document.atomic_path => document._index ? { "_id" => document.id } : true } } end
Get the atomic insert for embedded documents, either a push or set.
@example Get the inserts.
operation.inserts
@return [ Hash ] The insert ops.
@since 2.1.0
# File lib/mongoid/persistence/operations.rb, line 64 def inserts { document.atomic_insert_modifier => { document.atomic_position => document.as_document } } end
Should the parent document (in the case of embedded persistence) be notified of the child deletion. This is used when calling delete from the associations themselves.
@example Should the parent be notified?
operation.notifying_parent?
@return [ true, false ] If the parent should be notified.
@since 2.1.0
# File lib/mongoid/persistence/operations.rb, line 79 def notifying_parent? @notifying_parent ||= !@options.delete(:suppress) end
Get the parent of the provided document.
@example Get the parent.
operation.parent
@return [ Document ] The parent document.
@since 2.1.0
# File lib/mongoid/persistence/operations.rb, line 91 def parent document._parent end
Get the atomic selector for the document.
@example Get the selector.
operation.selector.
@return [ Hash ] The mongodb selector.
@since 2.1.0
# File lib/mongoid/persistence/operations.rb, line 103 def selector @selector ||= document.atomic_selector end
Get the atomic updates for the document without the conflicting modifications.
@example Get the atomic updates.
operation.updates
@return [ Hash ] The updates sans conflicting mods.
@since 2.1.0
# File lib/mongoid/persistence/operations.rb, line 116 def updates @updates ||= init_updates end
Should we be running validations on this persistence operation? Defaults to true.
@example Run validations?
operation.validating?
@return [ true, false ] If we run validations.
@since 2.1.0
# File lib/mongoid/persistence/operations.rb, line 129 def validating? @validating ||= @options[:validate].nil? ? true : @options[:validate] end