Sha256: 0e54d359cd6ed8ae694e4748469bbe16f55f23c119b79930193b4abcf1c4b8d9

Contents?: true

Size: 1.92 KB

Versions: 21

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

require "mongoid/clients/factory"
require "mongoid/clients/validators"
require "mongoid/clients/storage_options"
require "mongoid/clients/options"
require "mongoid/clients/sessions"

module Mongoid
  module Clients
    extend ActiveSupport::Concern
    include StorageOptions
    include Options
    include Sessions

    class << self

      # Clear all clients from the current thread.
      #
      # @example Clear all clients.
      #   Mongoid::Clients.clear
      #
      # @return [ Array ] The empty clients.
      #
      # @since 3.0.0
      def clear
        clients.clear
      end

      # Get the default client.
      #
      # @example Get the default client.
      #   Mongoid::Clients.default
      #
      # @return [ Mongo::Client ] The default client.
      #
      # @since 3.0.0
      def default
        with_name(:default)
      end

      # Disconnect all active clients.
      #
      # @example Disconnect all active clients.
      #   Mongoid::Clients.disconnect
      #
      # @return [ true ] True.
      #
      # @since 3.1.0
      def disconnect
        clients.values.each do |client|
          client.close
        end
      end

      # Get a client with the provided name.
      #
      # @example Get a client with the name.
      #   Mongoid::Clients.with_name(:replica)
      #
      # @param [ Symbol ] name The name of the client.
      #
      # @return [ Mongo::Client ] The named client.
      #
      # @since 3.0.0
      def with_name(name)
        name_as_symbol = name.to_sym
        return clients[name_as_symbol] if clients[name_as_symbol]
        CREATE_LOCK.synchronize do
          clients[name_as_symbol] ||= Clients::Factory.create(name)
        end
      end

      def set(name, client)
        clients[name.to_sym] = client
      end

      def clients
        @clients ||= {}
      end

      private

      CREATE_LOCK = Mutex.new
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
mongoid-7.1.11 lib/mongoid/clients.rb
mongoid-7.2.6 lib/mongoid/clients.rb
mongoid-7.2.5 lib/mongoid/clients.rb
mongoid-7.1.10 lib/mongoid/clients.rb
mongoid-7.1.9 lib/mongoid/clients.rb
mongoid-7.2.4 lib/mongoid/clients.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/lib/mongoid/clients.rb
mongoid-7.2.3 lib/mongoid/clients.rb
mongoid-7.1.8 lib/mongoid/clients.rb
mongoid-7.2.2 lib/mongoid/clients.rb
mongoid-7.2.1 lib/mongoid/clients.rb
mongoid-7.1.7 lib/mongoid/clients.rb
mongoid-7.2.0 lib/mongoid/clients.rb
mongoid-7.1.6 lib/mongoid/clients.rb
mongoid-7.1.5 lib/mongoid/clients.rb
mongoid-7.2.0.rc1 lib/mongoid/clients.rb
mongoid-7.1.4 lib/mongoid/clients.rb
mongoid-7.1.2 lib/mongoid/clients.rb
mongoid-7.1.1 lib/mongoid/clients.rb
mongoid-7.1.0 lib/mongoid/clients.rb