Class: Mongoid::Atomic::Paths::Embedded::Many

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Atomic::Paths::Embedded
Defined in:
build/mongoid-master/lib/mongoid/atomic/paths/embedded/many.rb

Overview

This class encapsulates behavior for locating and updating documents that are defined as an embedded 1-n.

Instance Attribute Summary

Attributes included from Mongoid::Atomic::Paths::Embedded

#delete_modifier, #document, #insert_modifier, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mongoid::Atomic::Paths::Embedded

#path

Constructor Details

#initialize(document) ⇒ Many

Create the new path utility.

Examples:

Create the path util.

Many.new(document)

Parameters:

  • document (Document)

    The document to generate the paths for.



19
20
21
22
# File 'build/mongoid-master/lib/mongoid/atomic/paths/embedded/many.rb', line 19

def initialize(document)
  @document, @parent = document, document._parent
  @insert_modifier, @delete_modifier ="$push", "$pull"
end

Class Method Details

.position_without_document(parent, association) ⇒ String

Get the position of where the document would go for the given association. The use case for this function is when trying to persist an empty list for an embedded association. All of the existing functions for getting the position to store a document require passing in a document to store, which we don't have when trying to store the empty list.

Parameters:

  • parent (Document)

    The parent document to store in.

  • association (Association)

    The association.

Returns:

  • (String)

    The position string.



51
52
53
54
# File 'build/mongoid-master/lib/mongoid/atomic/paths/embedded/many.rb', line 51

def position_without_document(parent, association)
  pos = parent.atomic_position
  "#{pos}#{"." unless pos.blank?}#{association.store_as}"
end

Instance Method Details

#positionString

Get the position of the document in the hierarchy. This will include indexes of 1-n embedded associations that may sit above the embedded many.

Examples:

Get the position.

many.position

Returns:

  • (String)

    The position of the document.



32
33
34
35
36
# File 'build/mongoid-master/lib/mongoid/atomic/paths/embedded/many.rb', line 32

def position
  pos = parent.atomic_position
  locator = document.new_record? ? "" : ".#{document._index}"
  "#{pos}#{"." unless pos.blank?}#{document._association.store_as}#{locator}"
end