Sha256: a82302ca277ce3612cb300db768a56b193e0ab4b59cd9c5b25be2f4c04ab0556

Contents?: true

Size: 1.83 KB

Versions: 11

Compression:

Stored size: 1.83 KB

Contents

# encoding: utf-8
require "mongoid/clients/factory"
require "mongoid/clients/validators"
require "mongoid/clients/storage_options"
require "mongoid/clients/options"

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

    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

11 entries across 9 versions & 2 rubygems

Version Path
mongoid-6.3.0 lib/mongoid/clients.rb
mongoid-6.2.1 lib/mongoid/clients.rb
mongoid-7.0.0.beta lib/mongoid/clients.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/clients.rb
tdiary-5.0.5 vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/clients.rb
tdiary-5.0.5 vendor/bundle/gems/mongoid-6.2.0/lib/mongoid/clients.rb
mongoid-6.2.0 lib/mongoid/clients.rb
mongoid-6.1.1 lib/mongoid/clients.rb
tdiary-5.0.4 vendor/bundle/gems/mongoid-6.1.0/lib/mongoid/clients.rb
mongoid-6.1.0 lib/mongoid/clients.rb
mongoid-6.1.0.rc0 lib/mongoid/clients.rb