Module: Mongoid::Clients
- Extended by:
- ActiveSupport::Concern
- Includes:
- Options, Sessions, StorageOptions
- Included in:
- Composable
- Defined in:
- lib/mongoid/clients.rb,
lib/mongoid/clients/factory.rb,
lib/mongoid/clients/options.rb,
lib/mongoid/clients/sessions.rb,
lib/mongoid/clients/storage_options.rb,
lib/mongoid/clients/validators/storage.rb
Overview
Mixin module included into Mongoid::Document which adds database client connection functionality. Also contains singleton class methods related to managing database clients.
Defined Under Namespace
Modules: Factory, Options, Sessions, StorageOptions, Validators
Constant Summary collapse
- CREATE_LOCK =
Mutex.new
Instance Attribute Summary
Attributes included from StorageOptions
Class Method Summary collapse
-
.clear ⇒ Array
Clear all clients from the current thread.
-
.clients ⇒ Hash<Symbol, Mongo::Client>
Returns the stored clients indexed by name.
-
.default ⇒ Mongo::Client
Get the default client.
-
.disconnect ⇒ true
Disconnect all active clients.
-
.reconnect ⇒ true
Reconnect all active clients.
-
.set(name, client) ⇒ Mongo::Client
Store a client with the provided name.
-
.with_name(name) ⇒ Mongo::Client
Get a stored client with the provided name.
Methods included from Sessions
Methods included from Options
#collection, #collection_name, #mongo_client, #persistence_context, #persistence_context?, #with
Methods included from StorageOptions
#remember_storage_options!, #storage_options
Class Method Details
.clear ⇒ Array
Clear all clients from the current thread.
26 27 28 |
# File 'lib/mongoid/clients.rb', line 26 def clear clients.clear end |
.clients ⇒ Hash<Symbol, Mongo::Client>
Returns the stored clients indexed by name.
101 102 103 |
# File 'lib/mongoid/clients.rb', line 101 def clients @clients ||= {} end |
.default ⇒ Mongo::Client
Get the default client.
36 37 38 |
# File 'lib/mongoid/clients.rb', line 36 def default with_name(:default) end |
.disconnect ⇒ true
Disconnect all active clients.
46 47 48 49 |
# File 'lib/mongoid/clients.rb', line 46 def disconnect clients.each_value(&:close) true end |
.reconnect ⇒ true
Reconnect all active clients.
57 58 59 60 |
# File 'lib/mongoid/clients.rb', line 57 def reconnect clients.each_value(&:reconnect) true end |
.set(name, client) ⇒ Mongo::Client
Store a client with the provided name.
94 95 96 |
# File 'lib/mongoid/clients.rb', line 94 def set(name, client) clients[name.to_sym] = client end |
.with_name(name) ⇒ Mongo::Client
Get a stored client with the provided name. If no client exists with the given name, a new one will be created, stored, and returned.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mongoid/clients.rb', line 72 def with_name(name) name_as_symbol = name.to_sym return clients[name_as_symbol] if clients[name_as_symbol] CREATE_LOCK.synchronize do if (key_vault_client = Mongoid.clients.dig(name_as_symbol, :options, :auto_encryption_options, :key_vault_client)) clients[key_vault_client.to_sym] ||= Clients::Factory.create(key_vault_client) end clients[name_as_symbol] ||= Clients::Factory.create(name) end end |