module ActiveLdap::Connection

def self.included(base)

def self.included(base)
  base.extend(ClassMethods)
end

def connected?

def connected?
  connection != self.class.connection
end

def connection

def connection
  conn = @connection
  return conn if conn
  have_dn = !@dn.nil?
  if !have_dn and attribute_name_resolvable_without_connection?
    begin
      have_dn = !get_attribute_before_type_cast(dn_attribute)[1].nil?
    rescue DistinguishedNameInvalid
    end
  end
  conn = self.class.active_connections[dn] || retrieve_connection if have_dn
  conn || self.class.connection
end

def connection=(adapter)

def connection=(adapter)
  if adapter.nil? or adapter.is_a?(Adapter::Base)
    @connection = adapter
  elsif adapter.is_a?(Hash)
    config = adapter
    @connection = self.class.instantiate_adapter(config)
  else
    setup_connection(adapter)
  end
end

def establish_connection(config=nil)

def establish_connection(config=nil)
  message =
    _("ActiveLdap::Connection#establish_connection has been deprecated " \
      "since 1.1.0. " \
      "Please use ActiveLdap::Connection#setup_connection instead.")
  ActiveLdap.deprecator.warn(message)
  setup_connection(config)
end

def remove_connection

def remove_connection
  self.class.remove_connection(dn)
  @connection = nil
end

def retrieve_connection

def retrieve_connection
  conn = self.class.active_connections[dn]
  return conn if conn
  config = self.class.configuration(dn)
  return nil unless config
  conn = self.class.instantiate_adapter(config)
  @connection = self.class.active_connections[dn] = conn
  conn
end

def schema

def schema
  connection.schema
end

def setup_connection(config=nil)

def setup_connection(config=nil)
  config = self.class.ensure_configuration(config)
  config = self.class.configuration.merge(config)
  config = self.class.merge_configuration(config, self)
  remove_connection
  self.class.define_configuration(dn, config)
end