module Mongoid::Persistence::Operations

Persistence operations include this module to get basic functionality on initialization.

Attributes

conflicts[R]
document[R]
options[R]

Public Class Methods

new(document, options = {}) click to toggle source

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

Public Instance Methods

collection() click to toggle source

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
deletes() click to toggle source

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
inserts() click to toggle source

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
notifying_parent?() click to toggle source

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
parent() click to toggle source

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
selector() click to toggle source

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
updates() click to toggle source

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
validating?() click to toggle source

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