123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Clients::Factory

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Defined in: lib/mongoid/clients/factory.rb

Overview

Factory used to create database clients.

Constant Summary

Class Attribute Summary

::Mongoid::Loggable - Extended

logger

Get the logger.

logger=

::Set the logger.

Class Method Summary

::Mongoid::Loggable - Extended

default_logger

Gets the default ::Mongoid logger - stderr.

rails_logger

Get the ::Rails logger if loaded in a ::Rails application, otherwise nil.

Instance Method Summary

Instance Method Details

#build_auto_encryption_options(opts, database) ⇒ Hash | nil (private)

This method is for 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.

Parameters:

  • opts (Hash)

    Options from the ::Mongoid client configuration.

  • database (String)

    Database name to use for encryption schema map.

Returns:

  • (Hash | nil)

    Auto encryption options for the client.

[ GitHub ]

  
# File 'lib/mongoid/clients/factory.rb', line 90

def build_auto_encryption_options(opts, database)
  return nil unless opts[:auto_encryption_options]

  opts[:auto_encryption_options].dup.tap do |auto_encryption_options|
    if auto_encryption_options.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
      auto_encryption_options[:schema_map] = Mongoid.config.encryption_schema_map(database)
    end
    if auto_encryption_options.key?(:key_vault_client)
      auto_encryption_options[:key_vault_client] = Mongoid.client(
        auto_encryption_options[: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.

Examples:

Create the client.

Factory.create(:analytics)

Parameters:

  • name (String | Symbol) (defaults to: nil)

    The named client configuration.

Returns:

  • (Mongo::Client)

    The new client.

Raises:

[ GitHub ]

  
# 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)

This method is for internal use only.

Create the client for the provided config.

Examples:

Create the client.

Factory.create_client(config)

Parameters:

  • configuration (Hash)

    The client config.

Returns:

  • (Mongo::Client)

    The client.

Raises:

[ GitHub ]

  
# 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(:options) || {}
  if opts.key?(:auto_encryption_options)
    opts[:auto_encryption_options] = build_auto_encryption_options(opts, database)
  end
  unless config.empty?
    default_logger.warn("Unknown config options detected: #{config}.")
  end
  if uri
    Mongo::Client.new(uri, options(opts))
  else
    Mongo::Client.new(
      hosts,
      options(opts).merge(database: database)
    )
  end
end

#defaultMongo::Client

Get the default client.

Examples:

Get the default client.

Factory.default

Returns:

  • (Mongo::Client)

    The default client.

Raises:

[ GitHub ]

  
# 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)

This method is for internal use only.

Prepare options for Mongo::Client based on ::Mongoid client configuration.

Parameters:

  • opts (Hash)

    Parameters from options section of ::Mongoid client configuration.

Returns:

  • (Hash)

    Options that should be passed to Mongo::Client constructor.

[ GitHub ]

  
# File 'lib/mongoid/clients/factory.rb', line 125

def options(opts)
  options = opts.dup
  options[:platform] = PLATFORM_DETAILS
  options[:app_name] = Mongoid::Config.app_name if Mongoid::Config.app_name
  if (driver_version <=> [2, 13]) >= 0
    wrap_lib = if options[:wrapping_libraries]
      [MONGOID_WRAPPING_LIBRARY] + options[:wrapping_libraries]
    else
      [MONGOID_WRAPPING_LIBRARY]
    end
    options[:wrapping_libraries] = wrap_lib
  end
  options.reject{ |k, _v| k == :hosts }.to_hash.symbolize_keys!
end