Module: Mongoid::Atomic

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/mongoid/atomic.rb,
lib/mongoid/atomic/modifiers.rb,
lib/mongoid/atomic/paths/root.rb,
lib/mongoid/atomic/paths/embedded.rb,
lib/mongoid/atomic/paths/embedded/one.rb,
lib/mongoid/atomic/paths/embedded/many.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Paths Classes: Modifiers

Constant Summary collapse

UPDATES =
[
  :atomic_array_pushes,
  :atomic_array_pulls,
  :atomic_array_add_to_sets,
  :atomic_pulls,
  :atomic_unsets,
  :delayed_atomic_sets,
  :delayed_atomic_pulls
]

Instance Method Summary collapse

Instance Method Details

#add_atomic_pull(document) ⇒ Object

Add the document as an atomic pull.

Examples:

Add the atomic pull.

person.add_atomic_pull(address)

Parameters:

  • The (Document)

    embedded document to pull.

Since:

  • 2.2.0



37
38
39
40
# File 'lib/mongoid/atomic.rb', line 37

def add_atomic_pull(document)
  document.flagged_for_destroy = true
  (delayed_atomic_pulls[document..name.to_s] ||= []).push(document)
end

#atomic_array_add_to_setsHash

For array fields these are the unique adds that need to happen.

Examples:

Get the array unique adds.

person.atomic_array_add_to_sets

Returns:

  • (Hash)

    The array add_to_sets.

Since:

  • 2.4.0



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

def atomic_array_add_to_sets
  @atomic_array_add_to_sets ||= {}
end

#atomic_array_pullsHash

For array fields these are the pulls that need to happen.

Examples:

Get the array pulls.

person.atomic_array_pulls

Returns:

  • (Hash)

    The array pulls.

Since:

  • 2.4.0



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

def atomic_array_pulls
  @atomic_array_pulls ||= {}
end

#atomic_array_pushesHash

For array fields these are the pushes that need to happen.

Examples:

Get the array pushes.

person.atomic_array_pushes

Returns:

  • (Hash)

    The array pushes.

Since:

  • 2.4.0



50
51
52
# File 'lib/mongoid/atomic.rb', line 50

def atomic_array_pushes
  @atomic_array_pushes ||= {}
end

#atomic_delete_modifierString

Get the removal modifier for the document. Will be nil on root documents, $unset on embeds_one, $set on embeds_many.

Examples:

Get the removal operator.

name.atomic_delete_modifier

Returns:

  • (String)

    The pull or unset operation.



118
119
120
# File 'lib/mongoid/atomic.rb', line 118

def atomic_delete_modifier
  atomic_paths.delete_modifier
end

#atomic_insert_modifierString

Get the insertion modifier for the document. Will be nil on root documents, $set on embeds_one, $push on embeds_many.

Examples:

Get the insert operation.

name.atomic_insert_modifier

Returns:

  • (String)

    The pull or set operator.



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

def atomic_insert_modifier
  atomic_paths.insert_modifier
end

#atomic_pathString

Return the path to this Document in JSON notation, used for atomic updates via $set in MongoDB.

Examples:

Get the path to this document.

address.atomic_path

Returns:

  • (String)

    The path to the document in the database.



140
141
142
# File 'lib/mongoid/atomic.rb', line 140

def atomic_path
  atomic_paths.path
end

#atomic_positionString

Returns the positional operator of this document for modification.

Examples:

Get the positional operator.

address.atomic_position

Returns:

  • (String)

    The positional operator with indexes.



150
151
152
# File 'lib/mongoid/atomic.rb', line 150

def atomic_position
  atomic_paths.position
end

#atomic_pullsArray<Hash>

Get all the attributes that need to be pulled.

Examples:

Get the pulls.

person.atomic_pulls

Returns:

Since:

  • 2.2.0



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/mongoid/atomic.rb', line 162

def atomic_pulls
  delayed_atomic_pulls.inject({}) do |pulls, (name, docs)|
    pulls.tap do |pull|
      docs.each do |doc|
        (pull[doc.atomic_path] ||= []).push(doc.as_document)
        doc.destroyed = true
        doc.flagged_for_destroy = false
      end
    end
  end
end

#atomic_pushesHash

Get all the push attributes that need to occur.

Examples:

Get the pushes.

person.atomic_pushes

Returns:

  • (Hash)

    The $pushAll operations.

Since:

  • 2.1.0



182
183
184
# File 'lib/mongoid/atomic.rb', line 182

def atomic_pushes
  pushable? ? { atomic_path => as_document } : {}
end

#atomic_selectorString

Return the selector for this document to be matched exactly for use with MongoDB’s $ operator.

Examples:

Get the selector.

address.atomic_selector

Returns:

  • (String)

    The exact selector for this document.



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

def atomic_selector
  atomic_paths.selector
end

#atomic_setsHash

Get all the attributes that need to be set.

Examples:

Get the sets.

person.atomic_sets

Returns:

  • (Hash)

    The $set operations.

Since:

  • 2.1.0



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

def atomic_sets
  updateable? ? setters : settable? ? { atomic_path => as_document } : {}
end

#atomic_unsetsArray<Hash>

Get all the attributes that need to be unset.

Examples:

Get the unsets.

person.atomic_unsets

Returns:

Since:

  • 2.2.0



217
218
219
# File 'lib/mongoid/atomic.rb', line 217

def atomic_unsets
  @atomic_unsets ||= []
end

#atomic_updatesHash Also known as: _updates

Note:

MongoDB does not allow “conflicting modifications” to be performed in a single operation. Conflicting modifications are detected by the ‘haveConflictingMod’ function in MongoDB. Examination of the code suggests that two modifications (a $set and a $pushAll, for example) conflict if:

(1) the key paths being modified are equal.
(2) one key path is a prefix of the other.

So a $set of ‘addresses.0.street’ will conflict with a $pushAll to ‘addresses’, and we will need to split our update into two pieces. We do not, however, attempt to match MongoDB’s logic exactly. Instead, we assume that two updates conflict if the first component of the two key paths matches.

Get all the atomic updates that need to happen for the current Document. This includes all changes that need to happen in the entire hierarchy that exists below where the save call was made.

Examples:

Get the updates that need to occur.

person.atomic_updates(children)

Returns:

  • (Hash)

    The updates and their modifiers.

Since:

  • 2.1.0



101
102
103
104
105
106
107
108
# File 'lib/mongoid/atomic.rb', line 101

def atomic_updates
  Modifiers.new.tap do |mods|
    generate_atomic_updates(mods, self)
    _children.each do |child|
      generate_atomic_updates(mods, child)
    end
  end
end

#delayed_atomic_pullsHash

Get a hash of atomic pulls that are pending.

Examples:

Get the atomic pulls.

document.delayed_atomic_pulls

Returns:

  • (Hash)

    name/document pairs.

Since:

  • 2.3.2



241
242
243
# File 'lib/mongoid/atomic.rb', line 241

def delayed_atomic_pulls
  @delayed_atomic_pulls ||= {}
end

#delayed_atomic_setsHash

Get all the atomic sets that have had their saves delayed.

Examples:

Get the delayed atomic sets.

person.delayed_atomic_sets

Returns:

  • (Hash)

    The delayed $sets.

Since:

  • 2.3.0



229
230
231
# File 'lib/mongoid/atomic.rb', line 229

def delayed_atomic_sets
  @delayed_atomic_sets ||= {}
end