Sha256: 953a8231f439651d7f15d1308461e4e48f564e50add9656e8dbedfc9051fa640

Contents?: true

Size: 1.89 KB

Versions: 21

Compression:

Stored size: 1.89 KB

Contents

# 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.0.13 lib/mongoid/clients.rb
mongoid-7.0.12 lib/mongoid/clients.rb
mongoid-6.4.8 lib/mongoid/clients.rb
mongoid-7.0.11 lib/mongoid/clients.rb
mongoid-7.0.10 lib/mongoid/clients.rb
mongoid-6.4.7 lib/mongoid/clients.rb
mongoid-6.4.5 lib/mongoid/clients.rb
mongoid-7.0.8 lib/mongoid/clients.rb
mongoid-7.0.7 lib/mongoid/clients.rb
mongoid-7.0.6 lib/mongoid/clients.rb
mongoid-7.0.5 lib/mongoid/clients.rb
mongoid-7.0.4 lib/mongoid/clients.rb
mongoid-6.4.4 lib/mongoid/clients.rb
mongoid-7.0.3 lib/mongoid/clients.rb
mongoid-6.4.2 lib/mongoid/clients.rb
mongoid-7.0.2 lib/mongoid/clients.rb
mongoid-7.0.1 lib/mongoid/clients.rb
mongoid-6.4.1 lib/mongoid/clients.rb
tdiary-5.0.8 vendor/bundle/gems/mongoid-6.4.0/lib/mongoid/clients.rb
mongoid-7.0.0 lib/mongoid/clients.rb