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
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.
29 30 31 |
# File 'lib/mongoid/clients.rb', line 29 def clear clients.clear end |
.clients ⇒ Hash<Symbol, Mongo::Client>
Returns the stored clients indexed by name.
102 103 104 |
# File 'lib/mongoid/clients.rb', line 102 def clients @clients ||= {} end |
.default ⇒ Mongo::Client
Get the default client.
39 40 41 |
# File 'lib/mongoid/clients.rb', line 39 def default with_name(:default) end |
.disconnect ⇒ true
Disconnect all active clients.
49 50 51 52 |
# File 'lib/mongoid/clients.rb', line 49 def disconnect clients.each_value(&:close) true end |
.reconnect ⇒ true
Reconnect all active clients.
60 61 62 63 |
# File 'lib/mongoid/clients.rb', line 60 def reconnect clients.each_value(&:reconnect) true end |
.set(name, client) ⇒ Mongo::Client
Store a client with the provided name.
95 96 97 |
# File 'lib/mongoid/clients.rb', line 95 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.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mongoid/clients.rb', line 75 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 |