<commit_before>require 'net/ldap'

require 'active_support/notifications'

require 'active_model/dirty'

require 'ldap/model/instrumentation'
require 'ldap/model/error'

module LDAP::Model
  class Base
    include ActiveModel::Dirty

    Error = LDAP::Model::Error # :nodoc:

    class << self
      delegate :logger, :logger=, :to => Instrumentation::LogSubscriber
    end

    def self.connection
      raise Error, "Connection not established" unless connected?
      @connection
    end

    def self.config
      @config
    end

    def self.establish_connection(config)
      config['port'] ||= 389

      url = "ldap%s://%s@%s:%d/" % [
        ('s' if config['encryption']),
        config['username'],
        config['hostname'],
        config['port']
      ]

      @config = config.dup.freeze

      instrument(:connect, :url => url) do
        @connection = Net::LDAP.new(
          base:       config['base'],
          host:       config['hostname'],
          port:       config['port'].to_i,
          encryption: config['encryption'],
          auth:       {
            method:   :simple,
            username: config['username'],
            password: config['password']
          }
        )

        unless @connection.bind
          reason = @connection.get_operation_result.message
          @connection = nil
          raise Error, "LDAP bind to #{config['hostname']} failed: #{reason}"
        end
      end

      true
    end

    def self.connected?
      !!@connection
    end

    def self.inherited(subclass)
      subclass.instance_variable_set(:@connection, @connection)
    end

    def self.all(options = {})
      base.inject([]) do |result, dn|
        result.concat(search(options.merge(base: dn)))
      end<commit_msg>Pass along base and scope to subclasses as well<commit_after>require 'net/ldap'

require 'active_support/notifications'

require 'active_model/dirty'

require 'ldap/model/instrumentation'
require 'ldap/model/error'

module LDAP::Model
  class Base
    include ActiveModel::Dirty

    Error = LDAP::Model::Error # :nodoc:

    class << self
      delegate :logger, :logger=, :to => Instrumentation::LogSubscriber
    end

    def self.connection
      raise Error, "Connection not established" unless connected?
      @connection
    end

    def self.config
      @config
    end

    def self.establish_connection(config)
      config['port'] ||= 389

      url = "ldap%s://%s@%s:%d/" % [
        ('s' if config['encryption']),
        config['username'],
        config['hostname'],
        config['port']
      ]

      @config = config.dup.freeze

      instrument(:connect, :url => url) do
        @connection = Net::LDAP.new(
          base:       config['base'],
          host:       config['hostname'],
          port:       config['port'].to_i,
          encryption: config['encryption'],
          auth:       {
            method:   :simple,
            username: config['username'],
            password: config['password']
          }
        )

        unless @connection.bind
          reason = @connection.get_operation_result.message
          @connection = nil
          raise Error, "LDAP bind to #{config['hostname']} failed: #{reason}"
        end
      end

      true
    end

    def self.connected?
      !!@connection
    end

    def self.inherited(subclass)
      %w( @connection @base @scope ).each do |ivar|
        subclass.instance_variable_set(ivar, instance_variable_get(ivar))
      end
    end

    def self.all(options = {})
      base.inject([]) do |result, dn|
        result.concat(search(options.merge(base: dn)))
      end
{'fraction_non_alphanumeric': 0.12417218543046357, 'fraction_numerical': 0.0016556291390728477, 'mean_word_length': 2.196649029982363, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '476371', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1034, 'n_words': 241}