Sha256: a7431ad4521354bd73ac3bd9cd4eb3ad0757ef17b04e85d49b49e6c4584f98a0

Contents?: true

Size: 1.47 KB

Versions: 70

Compression:

Stored size: 1.47 KB

Contents

# encoding: utf-8
module Mongoid

  # Provides behaviour for generating the selector for a specific document.
  #
  # @since 4.0.0
  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.
    #
    # @since 1.0.0
    def atomic_selector
      @atomic_selector ||=
        (embedded? ? embedded_atomic_selector : root_atomic_selector)
    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.
    #
    # @since 4.0.0
    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 for a root document.
    #
    # @api private
    #
    # @example Get the root atomic selector.
    #   document.root_atomic_selector
    #
    # @return [ Hash ] The root document selector.
    #
    # @since 4.0.0
    def root_atomic_selector
      { "_id" => _id }.merge!(shard_key_selector)
    end
  end
end

Version data entries

70 entries across 65 versions & 6 rubygems

Version Path
mongoid-6.0.1 lib/mongoid/selectable.rb
mongoid-5.1.5 lib/mongoid/selectable.rb
mongoid-6.0.0 lib/mongoid/selectable.rb
mongoid-5.1.4 lib/mongoid/selectable.rb
mongoid-6.0.0.rc0 lib/mongoid/selectable.rb
mongoid-6.0.0.beta lib/mongoid/selectable.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mongoid-4.0.2/lib/mongoid/selectable.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mongoid-5.1.3/lib/mongoid/selectable.rb
mongoid-5.1.3 lib/mongoid/selectable.rb
mongoid-5.1.2 lib/mongoid/selectable.rb
mongoid-5.1.1 lib/mongoid/selectable.rb
mongoid-5.1.0 lib/mongoid/selectable.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mongoid-5.0.2/lib/mongoid/selectable.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/mongoid-5.0.2/lib/mongoid/selectable.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mongoid-5.0.1/lib/mongoid/selectable.rb
mongoid-5.0.2 lib/mongoid/selectable.rb
mongoid-5.0.1 lib/mongoid/selectable.rb
mongoid-4.0.2 lib/mongoid/selectable.rb
mongoid-4.0.1 lib/mongoid/selectable.rb
mongoid-4.0.0 lib/mongoid/selectable.rb