Sha256: 5fe6a281e0e6e65e5a8377bdf6916ebffd09eb558525a3ec58038381f3e9b087

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Mongoid
  module Atomic
    module Paths
      module Embedded

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

          # Create the new path utility.
          #
          # @example Create the path util.
          #   Many.new(document)
          #
          # @param [ Document ] document The document to generate the paths for.
          def initialize(document)
            @document, @parent = document, document._parent
            @insert_modifier, @delete_modifier ="$push", "$pull"
          end

          # 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.
          #
          # @example Get the position.
          #   many.position
          #
          # @return [ String ] The position of the document.
          def position
            pos = parent.atomic_position
            locator = document.new_record? ? "" : ".#{document._index}"
            "#{pos}#{"." unless pos.blank?}#{document._association.store_as}#{locator}"
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongoid-7.5.2 lib/mongoid/atomic/paths/embedded/many.rb
mongoid-7.5.1 lib/mongoid/atomic/paths/embedded/many.rb
mongoid-7.5.0 lib/mongoid/atomic/paths/embedded/many.rb
mongoid-7.4.0 lib/mongoid/atomic/paths/embedded/many.rb