class Mongoid::PersistenceContext

and a client with particular options to use when persisting models.
Object encapsulating logic for setting/getting a collection and database name

def ==(other)

Returns:
  • (true | false) - Whether the two persistence contexts are equal.

Parameters:
  • other (Object) -- The object to be compared with this one.

Other tags:
    Example: Compare two persistence contexts. -
def ==(other)
  return false unless other.is_a?(PersistenceContext)
  options == other.options
end

def __evaluate__(name)

def __evaluate__(name)
  return nil unless name
  name.respond_to?(:call) ? name.call.to_sym : name.to_sym
end

def clear(object, cluster = nil, original_context = nil)

Parameters:
  • original_context (Mongoid::PersistenceContext) -- The original persistence
  • cluster (Mongo::Cluster) -- The original cluster before this context was used.
  • object (Class | Object) -- The class or model instance.

Other tags:
    Example: Clear the persistence context for a class or model instance. -
def clear(object, cluster = nil, original_context = nil)
  if context = get(object)
    unless cluster.nil? || context.cluster.equal?(cluster)
      context.client.close unless context.reusable_client?
    end
  end
ensure
  store_context(object, original_context)
end

def client

Returns:
  • (Mongo::Client) - The client for this persistence

Other tags:
    Example: Get the client for this persistence context. -
def client
  @client ||= begin
    client = Clients.with_name(client_name)
    options = client_options
    if database_name_option
      client = client.use(database_name)
      options = options.except(:database, 'database')
    end
    client = client.with(options) unless options.empty?
    client
  end
end

def client_name

Returns:
  • (Symbol) - The client name for this persistence

Other tags:
    Example: Get the client name for this persistence context. -
def client_name
  @client_name ||= __evaluate__(options[:client]) ||
                     Threaded.client_override ||
                     __evaluate__(storage_options[:client])
end

def client_options

def client_options
  @client_options ||= begin
    opts = options.select do |k, v|
                          Mongo::Client::VALID_OPTIONS.include?(k.to_sym)
                        end
    if opts[:read].is_a?(Symbol)
      opts[:read] = {mode: opts[:read]}
    end
    opts
  end
end

def collection(parent = nil)

Returns:
  • (Mongo::Collection) - The collection for this persistence

Parameters:
  • parent (Object) -- The parent object whose collection name is used

Other tags:
    Example: Get the collection for this persistence context. -
def collection(parent = nil)
  parent ?
    parent.collection.with(client_options.except(:database, "database")) :
    client[collection_name.to_sym]
end

def collection_name

Returns:
  • (String) - The collection name for this persistence

Other tags:
    Example: Get the collection name for this persistence context. -
def collection_name
  @collection_name ||= (__evaluate__(options[:collection] ||
                         storage_options[:collection]))
end

def context_store

def context_store
  Threaded.get(PERSISTENCE_CONTEXT_KEY) { {} }
end

def database_name

Returns:
  • (String) - The database name for this persistence

Other tags:
    Example: Get the database name for this persistence context. -
def database_name
  __evaluate__(database_name_option) || client.database.name
end

def database_name_option

def database_name_option
  @database_name_option ||= options[:database] ||
                              Threaded.database_override ||
                              storage_options[:database]
end

def for_child(document)

Other tags:
    Api: - private

Returns:
  • (PersistenceContext) - the new persistence context

Parameters:
  • document (Mongoid::Document | Class) -- the child document
def for_child(document)
  if document.is_a?(Class)
    return self if document == (@object.is_a?(Class) ? @object : @object.class)
  elsif document.is_a?(Mongoid::Document)
    return self if document.class == (@object.is_a?(Class) ? @object : @object.class)
  else
    raise ArgumentError, 'must specify a class or a document instance'
  end
  PersistenceContext.new(document, options.merge(document.storage_options))
end

def get(object)

Returns:
  • (Mongoid::PersistenceContext) - The persistence context for the object.

Parameters:
  • object (Object) -- The class or model instance.

Other tags:
    Example: Get the persistence context for a class or model instance. -
def get(object)
  get_context(object)
end

def get_context(object)

Other tags:
    Api: - private

Returns:
  • (Mongoid::PersistenceContext | nil) - The persistence context

Parameters:
  • object (Object) -- Object to get the persistance context for.
def get_context(object)
  context_store[object.object_id]
end

def initialize(object, opts = {})

Parameters:
  • opts (Hash) -- The persistence context options.
  • object (Object) -- The class or model instance for which a persistence context

Other tags:
    Example: Create a new persistence context. -
def initialize(object, opts = {})
  @object = object
  set_options!(opts)
end

def requested_storage_options

Other tags:
    Api: - private

Returns:
  • (Hash | nil) - the requested storage options, or nil if
def requested_storage_options
  slice = @options.slice(*Mongoid::Clients::Validators::Storage::VALID_OPTIONS)
  slice.any? ? slice : nil
end

def reusable_client?

Other tags:
    Api: - private

Returns:
  • (true | false) - True if client can be reused, otherwise false.
def reusable_client?
  @options.keys == [:client]
end

def set(object, options_or_context)

Returns:
  • (Mongoid::PersistenceContext) - The persistence context for the object.

Parameters:
  • options_or_context (Hash | Mongoid::PersistenceContext) -- The persistence
  • object (Object) -- The class or model instance.

Other tags:
    Example: Set the persistence context for a class or model instance. -
def set(object, options_or_context)
  existing_context = get_context(object)
  existing_options = if existing_context
    existing_context.options
  else
    {}
  end
  if options_or_context.is_a?(PersistenceContext)
    options_or_context = options_or_context.options
  end
  new_options = existing_options.merge(options_or_context)
  context = PersistenceContext.new(object, new_options)
  store_context(object, context)
end

def set_options!(opts)

def set_options!(opts)
  @options ||= opts.each.reduce({}) do |_options, (key, value)|
                 unless VALID_OPTIONS.include?(key.to_sym)
                   raise Errors::InvalidPersistenceOption.new(key.to_sym, VALID_OPTIONS)
                 end
                 value ? _options.merge!(key => value) : _options
               end
end

def store_context(object, context)

Other tags:
    Api: - private

Parameters:
  • context (Mongoid::PersistenceContext) -- Context to store
  • object (Object) -- Object to store the persistance context for.
def store_context(object, context)
  if context.nil?
    context_store.delete(object.object_id)
  else
    context_store[object.object_id] = context
  end
end