Module: Mongoid::Persistence::Atomic::Operation

Included in:
AddToSet, Bit, Inc, Pop, Pull, PullAll, Push, PushAll, Rename, Sets, Unset
Defined in:
lib/mongoid/persistence/atomic/operation.rb

Overview

This is the included module for all atomic operation objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#documentObject

Returns the value of attribute document.



9
10
11
# File 'lib/mongoid/persistence/atomic/operation.rb', line 9

def document
  @document
end

#fieldObject

Returns the value of attribute field.



9
10
11
# File 'lib/mongoid/persistence/atomic/operation.rb', line 9

def field
  @field
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/mongoid/persistence/atomic/operation.rb', line 9

def options
  @options
end

#valueObject

Returns the value of attribute value.



9
10
11
# File 'lib/mongoid/persistence/atomic/operation.rb', line 9

def value
  @value
end

Instance Method Details

#collectionCollection

Get the collection to be used for persistence.

Examples:

Get the collection.

operation.collection

Returns:

Since:

  • 2.1.0



19
20
21
# File 'lib/mongoid/persistence/atomic/operation.rb', line 19

def collection
  document._root.collection
end

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

Initialize the new pullAll operation.

Examples:

Create a new pullAll operation.

PullAll.new(document, :aliases, [ "Bond" ])

Parameters:

  • document (Document)

    The document to pullAll onto.

  • field (Symbol)

    The name of the array field.

  • value (Object)

    The value to pullAll.

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

    The persistence options.

Since:

  • 2.0.0



34
35
36
37
# File 'lib/mongoid/persistence/atomic/operation.rb', line 34

def initialize(document, field, value, options = {})
  @document, @field, @value = document, field.to_s, value
  @options = Safety.merge_safety_options(options)
end

#operation(modifier) ⇒ Hash

Get the atomic operation to perform.

Examples:

Get the operation.

inc.operation

Parameters:

  • modifier (String)

    The modifier to use.

Returns:

  • (Hash)

    The atomic operation for the field and addition.

Since:

  • 2.0.0



49
50
51
# File 'lib/mongoid/persistence/atomic/operation.rb', line 49

def operation(modifier)
  { modifier => { path => value } }
end

#pathString, Symbol

Get the path to the field that is getting atomically updated.

Examples:

Get the path.

operation.path

Returns:

Since:

  • 2.1.0



61
62
63
64
# File 'lib/mongoid/persistence/atomic/operation.rb', line 61

def path
  position = document.atomic_position
  position.blank? ? field : "#{position}.#{field}"
end

#prepareObject

All atomic operations use this with a block to ensure saftey options clear out after the execution.

Examples:

Prepare the operation.

prepare do
  collection.update
end

Returns:

  • (Object)

    The yielded value.

Since:

  • 2.1.0



77
78
79
80
81
# File 'lib/mongoid/persistence/atomic/operation.rb', line 77

def prepare
  yield(document).tap do
    Threaded.clear_options!
  end
end