Sha256: ede5b6229df977f5bfb89650d6a3b075001693ae8f45e688248ff160985055e4

Contents?: true

Size: 1.36 KB

Versions: 8

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Mongoid

  # Provides behavior for generating the selector for a specific document.
  module Selectable
    extend ActiveSupport::Concern

    # Get the atomic selector for the document. This is a hash in the simplest
    # case { "_id" => id }, but can become more complex for embedded documents
    # and documents that use a shard key.
    #
    # @example Get the document's atomic selector.
    #   document.atomic_selector
    #
    # @return [ Hash ] The document's selector.
    def atomic_selector
      @atomic_selector ||=
        (embedded? ? embedded_atomic_selector : root_atomic_selector_in_db)
    end

    private

    # Get the atomic selector for an embedded document.
    #
    # @api private
    #
    # @example Get the embedded atomic selector.
    #   document.embedded_atomic_selector
    #
    # @return [ Hash ] The embedded document selector.
    def embedded_atomic_selector
      if persisted? && _id_changed?
        _parent.atomic_selector
      else
        _parent.atomic_selector.merge("#{atomic_path}._id" => _id)
      end
    end

    # Get the atomic selector that would match the existing version of the
    # root document.
    #
    # @api private
    #
    # @return [ Hash ] The root document selector.
    def root_atomic_selector_in_db
      { "_id" => _id }.merge!(shard_key_selector_in_db)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mongoid-7.5.4 lib/mongoid/selectable.rb
mongoid-7.5.3 lib/mongoid/selectable.rb
mongoid-7.5.2 lib/mongoid/selectable.rb
mongoid-7.5.1 lib/mongoid/selectable.rb
mongoid-7.4.3 lib/mongoid/selectable.rb
mongoid-7.5.0 lib/mongoid/selectable.rb
mongoid-7.4.1 lib/mongoid/selectable.rb
mongoid-7.4.0 lib/mongoid/selectable.rb