Class: Mongoid::PersistenceContext
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Forwardable
|
|
Inherits: | Object |
Defined in: | lib/mongoid/persistence_context.rb |
Overview
Object encapsulating logic for setting/getting a collection and database name and a client with particular options to use when persisting models.
Constant Summary
-
EXTRA_OPTIONS =
Extra options in addition to driver client options that determine the persistence context.
[ :client, :collection, : ].freeze
-
PERSISTENCE_CONTEXT_KEY =
Internal use only
Key to store persistence contexts in the thread local storage.
:"[mongoid]:persistence_context"
-
VALID_OPTIONS =
The full list of valid persistence context options.
( Mongo::Client::VALID_OPTIONS + EXTRA_OPTIONS ).freeze
Class Method Summary
-
.clear(object, cluster = nil, original_context = nil)
Clear the persistence context for a particular class or model instance.
-
.get(object) ⇒ Mongoid::PersistenceContext
Get the persistence context for a particular class or model instance.
-
.new(object, opts = {}) ⇒ PersistenceContext
constructor
Initialize the persistence context object.
-
.set(object, options_or_context) ⇒ Mongoid::PersistenceContext
::Set
the persistence context for a particular class or model instance. -
.get_context(object) ⇒ Mongoid::PersistenceContext | nil
private
Internal use only
Internal use only
Get the persistence context for a given object from the thread local.
-
.store_context(object, context)
private
Internal use only
Internal use only
Store persistence context for a given object in the thread local.
Instance Attribute Summary
-
#options ⇒ Hash
readonly
The options defining this persistence context.
-
#reusable_client? ⇒ true | false
readonly
Internal use only
Internal use only
Whether the client of the context can be reused later, and therefore should not be closed.
Instance Method Summary
-
#==(other) ⇒ true | false
Determine if this persistence context is equal to another.
-
#client ⇒ Mongo::Client
Get the client for this persistence context.
-
#client_name ⇒ Symbol
Get the client name for this persistence context.
-
#collection(parent = nil) ⇒ Mongo::Collection
Get the collection for this persistence context.
-
#collection_name ⇒ String
Get the collection name for this persistence context.
-
#database_name ⇒ String
Get the database name for this persistence context.
-
#for_child(document) ⇒ PersistenceContext
Internal use only
Internal use only
Returns a new persistence context that is consistent with the given child document, inheriting most appropriate settings.
-
#requested_storage_options ⇒ Hash | nil
Internal use only
Internal use only
The subset of provided options that may be used as storage options.
- #__evaluate__(name) private
- #client_options private
- #database_name_option private
- #set_options!(opts) private
Constructor Details
.new(object, opts = {}) ⇒ PersistenceContext
Initialize the persistence context object.
# File 'lib/mongoid/persistence_context.rb', line 46
def initialize(object, opts = {}) @object = object (opts) end
Class Method Details
.clear(object, cluster = nil, original_context = nil)
Clear the persistence context for a particular class or model instance.
# File 'lib/mongoid/persistence_context.rb', line 268
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
.get(object) ⇒ PersistenceContext
Get the persistence context for a particular class or model instance.
# File 'lib/mongoid/persistence_context.rb', line 255
def get(object) get_context(object) end
.get_context(object) ⇒ PersistenceContext
| nil
(private)
Get the persistence context for a given object from the thread local
storage.
# File 'lib/mongoid/persistence_context.rb', line 294
def get_context(object) Thread.current[PERSISTENCE_CONTEXT_KEY] ||= {} Thread.current[PERSISTENCE_CONTEXT_KEY][object.object_id] end
.set(object, options_or_context) ⇒ PersistenceContext
::Set
the persistence context for a particular class or model instance.
If there already is a persistence context set, options in the existing context are combined with options given to the set call.
# File 'lib/mongoid/persistence_context.rb', line 232
def set(object, ) existing_context = get_context(object) = if existing_context existing_context. else {} end if .is_a?(PersistenceContext) = . end = .merge( ) context = PersistenceContext.new(object, ) store_context(object, context) end
.store_context(object, context) (private)
Store persistence context for a given object in the thread local
storage.
# File 'lib/mongoid/persistence_context.rb', line 306
def store_context(object, context) if context.nil? Thread.current[PERSISTENCE_CONTEXT_KEY]&.delete(object.object_id) else Thread.current[PERSISTENCE_CONTEXT_KEY] ||= {} Thread.current[PERSISTENCE_CONTEXT_KEY][object.object_id] = context end end
Instance Attribute Details
#options ⇒ Hash (readonly)
The options defining this persistence context.
# File 'lib/mongoid/persistence_context.rb', line 20
attr_reader :
#reusable_client? ⇒ true
| false
(readonly)
Whether the client of the context can be reused later, and therefore should not be closed.
If the persistence context is requested with :client
option only, it means that the context should use a client configured in mongoid.yml. Such clients should not be closed when the context is cleared since they will be reused later.
# File 'lib/mongoid/persistence_context.rb', line 167
def reusable_client? @options.keys == [:client] end
Instance Method Details
#==(other) ⇒ true
| false
Determine if this persistence context is equal to another.
# File 'lib/mongoid/persistence_context.rb', line 151
def ==(other) return false unless other.is_a?(PersistenceContext) == other. end
#__evaluate__(name) (private)
[ GitHub ]# File 'lib/mongoid/persistence_context.rb', line 194
def __evaluate__(name) return nil unless name name.respond_to?(:call) ? name.call.to_sym : name.to_sym end
#client ⇒ Mongo::Client
Get the client for this persistence context.
# File 'lib/mongoid/persistence_context.rb', line 117
def client @client ||= begin client = Clients.with_name(client_name) if database_name_option client = client.use(database_name) end unless .empty? client = client.with( ) end client end end
#client_name ⇒ Symbol
Get the client name for this persistence context.
# File 'lib/mongoid/persistence_context.rb', line 137
def client_name @client_name ||= [:client] || Threaded.client_override || __evaluate__( [:client]) end
#client_options (private)
[ GitHub ]# File 'lib/mongoid/persistence_context.rb', line 199
def @client_options ||= begin opts = .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
#collection(parent = nil) ⇒ Mongo::Collection
Get the collection for this persistence context.
# File 'lib/mongoid/persistence_context.rb', line 81
def collection(parent = nil) parent ? parent.collection.with( .except(:database, "database")) : client[collection_name.to_sym] end
#collection_name ⇒ String
Get the collection name for this persistence context.
# File 'lib/mongoid/persistence_context.rb', line 94
def collection_name @collection_name ||= (__evaluate__( [:collection] || [:collection])) end
#database_name ⇒ String
Get the database name for this persistence context.
# File 'lib/mongoid/persistence_context.rb', line 106
def database_name __evaluate__(database_name_option) || client.database.name end
#database_name_option (private)
[ GitHub ]# File 'lib/mongoid/persistence_context.rb', line 211
def database_name_option @database_name_option ||= [:database] || Threaded.database_override || [:database] end
#for_child(document) ⇒ PersistenceContext
Returns a new persistence context that is consistent with the given child document, inheriting most appropriate settings.
# File 'lib/mongoid/persistence_context.rb', line 59
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, .merge(document. )) end
#requested_storage_options ⇒ Hash | nil
The subset of provided options that may be used as storage options.
# File 'lib/mongoid/persistence_context.rb', line 178
def slice = @options.slice(*Mongoid::Clients::Validators::Storage::VALID_OPTIONS) slice.any? ? slice : nil end
#set_options!(opts) (private)
[ GitHub ]# File 'lib/mongoid/persistence_context.rb', line 185
def (opts) @options ||= opts.each.reduce({}) do |, (key, value)| unless VALID_OPTIONS.include?(key.to_sym) raise Errors::InvalidPersistenceOption.new(key.to_sym, VALID_OPTIONS) end value ? .merge!(key => value) : end end