Module: Mongoid::Clients::Factory
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
::Mongoid::Loggable ,
self
|
|
Defined in: | lib/mongoid/clients/factory.rb |
Overview
Factory
used to create database clients.
Constant Summary
-
MONGOID_WRAPPING_LIBRARY =
# File 'lib/mongoid/clients/factory.rb', line 110{ name: 'Mongoid', version: VERSION, }.freeze
Class Attribute Summary
::Mongoid::Loggable
- Extended
Class Method Summary
::Mongoid::Loggable
- Extended
default_logger | Gets the default |
rails_logger |
Instance Method Summary
-
#create(name = nil) ⇒ Mongo::Client
Create a new client given the named configuration.
-
#default ⇒ Mongo::Client
Get the default client.
-
#build_auto_encryption_options(opts, database) ⇒ Hash | nil
private
Internal use only
Internal use only
Build auto encryption options for the client based on the options provided in the
::Mongoid
client configuration and the encryption schema map for the database. -
#create_client(configuration) ⇒ Mongo::Client
private
Internal use only
Internal use only
Create the client for the provided config.
- #driver_version private
-
#options(opts) ⇒ Hash
private
Internal use only
Internal use only
Prepare options for Mongo::Client based on
::Mongoid
client configuration.
Instance Method Details
#build_auto_encryption_options(opts, database) ⇒ Hash | nil
(private)
Build auto encryption options for the client based on the options provided in the ::Mongoid
client configuration and the encryption schema map for the database.
# File 'lib/mongoid/clients/factory.rb', line 90
def (opts, database) return nil unless opts[: ] opts[: ].dup.tap do || if .key?(:schema_map) default_logger.warn( 'The :schema_map is configured in the :auto_encryption_options for the client;' + ' encryption setting in Mongoid documents will be ignored.' ) else [:schema_map] = Mongoid.config.encryption_schema_map(database) end if .key?(:key_vault_client) [:key_vault_client] = Mongoid.client( [:key_vault_client] ) end end end
#create(name = nil) ⇒ Mongo::Client
Create a new client given the named configuration. If no name is provided, return a new client with the default configuration. If a name is provided for which no configuration exists, an error will be raised.
# File 'lib/mongoid/clients/factory.rb', line 25
def create(name = nil) return default unless name config = Mongoid.clients[name] raise Errors::NoClientConfig.new(name) unless config create_client(config) end
#create_client(configuration) ⇒ Mongo::Client
(private)
Create the client for the provided config.
# File 'lib/mongoid/clients/factory.rb', line 57
def create_client(configuration) raise Errors::NoClientsConfig.new unless configuration config = configuration.dup uri = config.delete(:uri) database = config.delete(:database) || Mongo::URI.get(uri).database hosts = config.delete(:hosts) opts = config.delete(: ) || {} if opts.key?(: ) opts[: ] = (opts, database) end unless config.empty? default_logger.warn("Unknown config options detected: #{config}.") end if uri Mongo::Client.new(uri, (opts)) else Mongo::Client.new( hosts, (opts).merge(database: database) ) end end
#default ⇒ Mongo::Client
Get the default client.
# File 'lib/mongoid/clients/factory.rb', line 41
def default create_client(Mongoid.clients[:default]) end
#driver_version (private)
[ GitHub ]# File 'lib/mongoid/clients/factory.rb', line 115
def driver_version Mongo::VERSION.split('.')[0...2].map(&:to_i) end
#options(opts) ⇒ Hash (private)
Prepare options for Mongo::Client based on ::Mongoid
client configuration.
# File 'lib/mongoid/clients/factory.rb', line 125
def (opts) = opts.dup [:platform] = PLATFORM_DETAILS [:app_name] = Mongoid::Config.app_name if Mongoid::Config.app_name if (driver_version <=> [2, 13]) >= 0 wrap_lib = if [:wrapping_libraries] [MONGOID_WRAPPING_LIBRARY] + [:wrapping_libraries] else [MONGOID_WRAPPING_LIBRARY] end [:wrapping_libraries] = wrap_lib end .reject{ |k, _v| k == :hosts }.to_hash.symbolize_keys! end