Sha256: 485d635b38aa6142a825962f3261b4b5959dcab61f40649c6624d680b4d8256f

Contents?: true

Size: 1.7 KB

Versions: 9

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require "mongoid/contextual/queryable"
require "mongoid/contextual/mongo"
require "mongoid/contextual/memory"
require "mongoid/contextual/none"

module Mongoid
  module Contextual
    extend Forwardable

    # The aggregate operations provided in the aggregate module get delegated
    # through to the context from the criteria.
    def_delegators :context, *Aggregable::Mongo.public_instance_methods(false)

    # The atomic operations provided in the atomic context get delegated
    # through to the context from the criteria.
    def_delegators :context, *Atomic.public_instance_methods(false)

    # The methods in the contexts themselves should all get delegated to,
    # including destructive, modification, and optional methods.
    def_delegators :context, *(Mongo.public_instance_methods(false) - [ :skip, :limit ])

    # This gets blank and empty included.
    def_delegators :context, *Queryable.public_instance_methods(false)

    # Get the context in which criteria queries should execute. This is either
    # in memory (for embedded documents) or mongo (for root level documents.)
    #
    # @example Get the context.
    #   criteria.context
    #
    # @return [ Memory, Mongo ] The context.
    def context
      @context ||= create_context
    end

    private

    # Create the context for the queries to execute. Will be memory for
    # embedded documents and mongo for root documents.
    #
    # @api private
    #
    # @example Create the context.
    #   contextual.create_context
    #
    # @return [ Mongo, Memory ] The context.
    def create_context
      return None.new(self) if empty_and_chainable?
      embedded ? Memory.new(self) : Mongo.new(self)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

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