Module: Mongoid::Persistence::Operations

Included in:
Embedded::Insert, Embedded::Remove, Insert, Remove, Update, Upsert
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/upsert.rb,
lib/mongoid/persistence/operations/embedded/insert.rb,
lib/mongoid/persistence/operations/embedded/remove.rb

Overview

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

Defined Under Namespace

Modules: Embedded Classes: Insert, Remove, Update, Upsert

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#conflictsObject (readonly)

Returns the value of attribute conflicts.



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

def conflicts
  @conflicts
end

#documentObject (readonly)

Returns the value of attribute document.



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

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
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



178
179
180
# File 'lib/mongoid/persistence/operations.rb', line 178

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



163
164
165
# File 'lib/mongoid/persistence/operations.rb', line 163

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



193
194
195
# File 'lib/mongoid/persistence/operations.rb', line 193

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

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

Get the appropriate upsert operation based on the document.

Examples:

Get the upsert operation.

Operations.upsert(doc, options)

Parameters:

  • doc (Document)

    The document to persist.

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

    The persistence options.

Returns:

Since:

  • 3.0.0



208
209
210
# File 'lib/mongoid/persistence/operations.rb', line 208

def upsert(doc, options = {})
  Upsert.new(doc, options)
end

Instance Method Details

#collectionCollection

Get the collection we should be persisting to.

Examples:

Get the collection.

operation.collection

Returns:

  • (Collection)

    The collection to persist to.

Since:

  • 2.1.0



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

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



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

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



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

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



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

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



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

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

#parentDocument

Get the parent of the provided document.

Examples:

Get the parent.

operation.parent

Returns:

Since:

  • 2.1.0



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

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



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

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



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

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



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

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