Module: Mongoid::Persistence::Operations

Included in:
Embedded::Insert, Embedded::Remove, Insert, Remove, Update
Defined in:
lib/mongoid/persistence/operations.rb,
lib/mongoid/persistence/operations/insert.rb,
lib/mongoid/persistence/operations/remove.rb,
lib/mongoid/persistence/operations/update.rb,
lib/mongoid/persistence/operations/embedded/insert.rb,
lib/mongoid/persistence/operations/embedded/remove.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Embedded Classes: Insert, Remove, Update

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#conflictsObject (readonly)

Returns the value of attribute conflicts.



15
16
17
# File 'lib/mongoid/persistence/operations.rb', line 15

def conflicts
  @conflicts
end

#documentObject (readonly)

Returns the value of attribute document.



15
16
17
# File 'lib/mongoid/persistence/operations.rb', line 15

def document
  @document
end

Class Method Details

.insert(doc, options = {}) ⇒ Operations

Get the appropriate insertion operation based on the document.

Examples:

Get the insertion operation.

Operations.insert(doc, options)

Parameters:

  • doc (Document)

    The document to persist.

  • options (Hash) (defaults to: {})

    The persistence options.

Returns:

Since:

  • 2.1.0



190
191
192
# File 'lib/mongoid/persistence/operations.rb', line 190

def insert(doc, options = {})
  (doc.embedded? ? Embedded::Insert : Insert).new(doc, options)
end

.remove(doc, options = {}) ⇒ Operations

Get the appropriate removal operation based on the document.

Examples:

Get the deletion operation.

Operations.remove(doc, options)

Parameters:

  • doc (Document)

    The document to persist.

  • options (Hash) (defaults to: {})

    The persistence options.

Returns:

Since:

  • 2.1.0



175
176
177
# File 'lib/mongoid/persistence/operations.rb', line 175

def remove(doc, options = {})
  (doc.embedded? ? Embedded::Remove : Remove).new(doc, options)
end

.update(doc, options = {}) ⇒ Operations

Get the appropriate update operation based on the document.

Examples:

Get the update operation.

Operations.update(doc, options)

Parameters:

  • doc (Document)

    The document to persist.

  • options (Hash) (defaults to: {})

    The persistence options.

Returns:

Since:

  • 2.1.0



205
206
207
# File 'lib/mongoid/persistence/operations.rb', line 205

def update(doc, options = {})
  Update.new(doc, options)
end

Instance Method Details

#collectionCollection

Get the collection we should be persisting to.

Examples:

Get the collection.

operation.collection

Returns:

Since:

  • 2.1.0



25
26
27
# File 'lib/mongoid/persistence/operations.rb', line 25

def collection
  @collection ||= document._root.collection
end

#deletesHash

Get the atomic delete operations for embedded documents.

Examples:

Get the atomic deletes.

operation.deletes

Returns:

  • (Hash)

    The atomic delete selector.

Since:

  • 2.1.0



37
38
39
40
41
# File 'lib/mongoid/persistence/operations.rb', line 37

def deletes
  { document.atomic_delete_modifier =>
    { document.atomic_path =>
      document._index ? { "_id" => document.id } : true } }
end

#initialize(document, options = {}) ⇒ Object

Instantiate the new persistence operation.

Examples:

Create the operation.

Operation.new(document, { :safe => true }, { "field" => "value" })

Parameters:

  • document (Document)

    The document to persist.

  • options (Hash) (defaults to: {})

    The persistence options.

Since:

  • 2.1.0



52
53
54
# File 'lib/mongoid/persistence/operations.rb', line 52

def initialize(document, options = {})
  @document, @options = document, options
end

#insertsHash

Get the atomic insert for embedded documents, either a push or set.

Examples:

Get the inserts.

operation.inserts

Returns:

  • (Hash)

    The insert ops.

Since:

  • 2.1.0



64
65
66
67
# File 'lib/mongoid/persistence/operations.rb', line 64

def inserts
  { document.atomic_insert_modifier =>
    { document.atomic_position => document.as_document } }
end

#notifying_parent?true, false

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.

Examples:

Should the parent be notified?

operation.notifying_parent?

Returns:

  • (true, false)

    If the parent should be notified.

Since:

  • 2.1.0



79
80
81
# File 'lib/mongoid/persistence/operations.rb', line 79

def notifying_parent?
  @notifying_parent ||= !@options.delete(:suppress)
end

#optionsHash

Get all the options that will be sent to the database. Right now this is only safe mode opts.

Examples:

Get the options hash.

operation.options

Returns:

  • (Hash)

    The options for the database.

Since:

  • 2.1.0



92
93
94
# File 'lib/mongoid/persistence/operations.rb', line 92

def options
  Safety.merge_safety_options(@options)
end

#parentDocument

Get the parent of the provided document.

Examples:

Get the parent.

operation.parent

Returns:

Since:

  • 2.1.0



104
105
106
# File 'lib/mongoid/persistence/operations.rb', line 104

def parent
  document._parent
end

#selectorHash

Get the atomic selector for the document.

Examples:

Get the selector.

operation.selector.

Returns:

  • (Hash)

    The mongodb selector.

Since:

  • 2.1.0



116
117
118
# File 'lib/mongoid/persistence/operations.rb', line 116

def selector
  @selector ||= document.atomic_selector
end

#updatesHash

Get the atomic updates for the document without the conflicting modifications.

Examples:

Get the atomic updates.

operation.updates

Returns:

  • (Hash)

    The updates sans conflicting mods.

Since:

  • 2.1.0



129
130
131
# File 'lib/mongoid/persistence/operations.rb', line 129

def updates
  @updates ||= init_updates
end

#validating?true, false

Should we be running validations on this persistence operation? Defaults to true.

Examples:

Run validations?

operation.validating?

Returns:

  • (true, false)

    If we run validations.

Since:

  • 2.1.0



142
143
144
# File 'lib/mongoid/persistence/operations.rb', line 142

def validating?
  @validating ||= @options[:validate].nil? ? true : @options[:validate]
end