<commit_before>require_relative "./classloader.rb"
require_relative "./logging.rb"
require_relative "./error.rb"

class CalculatorLoaderContext < ClassLoaderContext
  include Logging

  FIELD_PREFIX = :field_

  def initialize(classloader, fields = {})
    super classloader

    @fields = fields
  end

  def fields=(fields)
    @fields = fields
  end

  def fields
    @fields
  end

  def field?(field_name, prefixed = true)
    fields.include? field_name(field_name, prefixed)
  end

  def field_name(name, reverse = false)
    if reverse
      name.to_sym.sub /^#{FIELD_PREFIX.to_s}/, ""
    else
      FIELD_PREFIX + name.to_sym
    end
  end

  def method_missing(symbol, *arguments)
    if symbol.start_with? FIELD_PREFIX
      reversed_name = field_name(symbol, true)

      if !fields.include? reversed_name
        raise NoFieldError, reversed_name
      end

      fields[reversed_name]
    else
      super
    end
  end

  def respond_to?(symbol, include_private = false)
    if include_private && symbol.start_with?(FIELD_PREFIX)
      fields.include? field_name(symbol, true)
    else
      super
    end
  end
end

class CalculatorLoader < ClassLoader
  include Logging

  CALC_EXT = ".rb"

  def initialize
    super CalculatorLoaderContext

    @file_dates = {}
  end

  def load_directory(directory)
    logger.debug "Loading directory `#{directory}'"

    Dir.glob(File.join(directory, "*#{CALC_EXT}")).each { |filename| load_file filename }
  end

  def load_calculators(directory)
    logger.debug "Loading calculators from `#{directory}'"

    directories = Dir.glob(File.join(directory, "*")).select do |filename|
      if File.directory? filename
        load_directory filename
      end
    end<commit_msg>Use #each instead of #select<commit_after>require_relative "./classloader.rb"
require_relative "./logging.rb"
require_relative "./error.rb"

class CalculatorLoaderContext < ClassLoaderContext
  include Logging

  FIELD_PREFIX = :field_

  def initialize(classloader, fields = {})
    super classloader

    @fields = fields
  end

  def fields=(fields)
    @fields = fields
  end

  def fields
    @fields
  end

  def field?(field_name, prefixed = true)
    fields.include? field_name(field_name, prefixed)
  end

  def field_name(name, reverse = false)
    if reverse
      name.to_sym.sub /^#{FIELD_PREFIX.to_s}/, ""
    else
      FIELD_PREFIX + name.to_sym
    end
  end

  def method_missing(symbol, *arguments)
    if symbol.start_with? FIELD_PREFIX
      reversed_name = field_name(symbol, true)

      if !fields.include? reversed_name
        raise NoFieldError, reversed_name
      end

      fields[reversed_name]
    else
      super
    end
  end

  def respond_to?(symbol, include_private = false)
    if include_private && symbol.start_with?(FIELD_PREFIX)
      fields.include? field_name(symbol, true)
    else
      super
    end
  end
end

class CalculatorLoader < ClassLoader
  include Logging

  CALC_EXT = ".rb"

  def initialize
    super CalculatorLoaderContext

    @file_dates = {}
  end

  def load_directory(directory)
    logger.debug "Loading directory `#{directory}'"

    Dir.glob(File.join(directory, "*#{CALC_EXT}")).each { |filename| load_file filename }
  end

  def load_calculators(directory)
    logger.debug "Loading calculators from `#{directory}'"

    Dir.glob(File.join(directory, "*")).each do |filename|
      if File.directory? filename
        load_directory filename
      end
    end
{'fraction_non_alphanumeric': 0.09143351600807614, 'fraction_numerical': 0.0, 'mean_word_length': 3.193470374848851, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2229876', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1061, 'n_words': 264}