123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Contextual

Overview

Parent mixin module which adds aggregation (#sum, #avg, etc.) and atomic (#set, #unset, #push, etc.) behavior to Criteria.

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#short_circuit_query?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/mongoid/contextual.rb', line 70

def short_circuit_query?
  return false unless Mongoid.allow_short_circuit_queries?

  selector.any? do |_field, condition|
    next unless condition.is_a?(Hash)

    v = condition['$in'] || condition[:$in]
    v.is_a?(Array) && v.empty?
  end
end

Instance Method Details

#contextMemory | Mongo

Get the context in which criteria queries should execute. This is either in memory (for embedded documents) or mongo (for root level documents.)

Examples:

Get the context.

criteria.context

Returns:

[ GitHub ]

  
# File 'lib/mongoid/contextual.rb', line 36

def context
  @context ||= create_context
end

#create_contextMongo | Memory (private)

This method is for internal use only.

Create the context for the queries to execute. Will be memory for embedded documents and mongo for root documents.

Examples:

Create the context.

contextual.create_context

Returns:

[ GitHub ]

  
# File 'lib/mongoid/contextual.rb', line 63

def create_context
  return None.new(self) if empty_and_chainable?
  return None.new(self) if short_circuit_query?

  embedded ? Memory.new(self) : Mongo.new(self)
end

#load_asyncCriteria

Instructs the context to schedule an asynchronous loading of documents specified by the criteria.

Note that depending on the context and on the ::Mongoid configuration, documents can be loaded synchronously on the caller's thread.

Returns:

[ GitHub ]

  
# File 'lib/mongoid/contextual.rb', line 47

def load_async
  context.load_async if context.respond_to?(:load_async)
  self
end