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

Includes:
Atomic::Positionable
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

Methods included from Atomic::Positionable

#positionally

Instance Attribute Details

#documentObject

Returns the value of attribute document.



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

def document
  @document
end

#fieldsObject

Returns the value of attribute fields.



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

def fields
  @fields
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#collectionCollection

Get the collection to be used for persistence.

Examples:

Get the collection.

operation.collection

Returns:

  • (Collection)

    The root collection.

Since:

  • 2.1.0



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

def collection
  document._root.collection
end

#initialize(document, fields, 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



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mongoid/persistence/atomic/operation.rb', line 35

def initialize(document, fields, value, options = {})
  @document, @value = document, value
  @options = options

  @fields = Array.wrap(fields).collect do |field|
    document.database_field_name(field.to_s)
  end

  self.class.send(:define_method, :field) do
    @fields.first
  end if @fields.length == 1

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



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

def operation(modifier)
  hash = Hash[fields.collect do |field|
    [path(field), cast_value]
  end]
  { modifier => hash }
end

#path(field = field) ⇒ String, Symbol

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

Examples:

Get the path.

operation.path

Returns:

  • (String, Symbol)

    The path to the field.

Since:

  • 2.1.0



74
75
76
77
# File 'lib/mongoid/persistence/atomic/operation.rb', line 74

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

#prepare {|document| ... } ⇒ Object

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

Yields:

Returns:

  • (Object)

    The yielded value.

Since:

  • 2.1.0



90
91
92
# File 'lib/mongoid/persistence/atomic/operation.rb', line 90

def prepare
  yield(document)
end