<commit_before>module Dossier
  class Result
    include Enumerable

    attr_accessor :report, :adapter_results

    def initialize(adapter_results, report)
      self.adapter_results = adapter_results
      self.report          = report
    end

    def raw_headers
      @raw_headers ||= adapter_results.headers
    end

    def headers
      raise NotImplementedError.new("#{self.class.name} must implement `headers', use `raw_headers' for adapter headers")
    end

    def body
      size = rows.length - report.options[:footer].to_i
      @body ||= size < 0 ? [] : rows.first(size)
    end

    def footers
      @footer ||= rows.last(report.options[:footer].to_i)
    end

    def rows
      @rows ||= to_a
    end

    def arrays
      @arrays ||= [headers] + rows
    end

    def hashes
      return @hashes if defined?(@hashes)
      @hashes = rows.map { |row| row_hash(row) }
    end

    # this is the method that creates the individual hash entry
    # hashes should always use raw headers
    def row_hash(row)
      Hash[self.report.columns.map {|v| [v,row.send(v)]}]
    end

    def each
      raise NotImplementedError.new("#{self.class.name} must define `each`")
    end

    class Formatted < Result

      def headers
              @formatted_headers ||= self.report.columns.map { |h| report.format_header(h) }
      end

      def each
        adapter_results.rows.each { |row| yield format(row) }
      end

      def format(row)
       
        self.report.columns.each_with_index.map do |value, i|
          
          if row.is_a?(ActiveRecord::Base)
            column = value
            method = "format_#{column}"
            value  = row.send(value)
          else
            column = raw_headers.at(i)
            method = "format_#{column}"
          end

          if report.respond_to?(method)<commit_msg>handle options for dynamic values<commit_after>module Dossier
  class Result
    include Enumerable

    attr_accessor :report, :adapter_results

    def initialize(adapter_results, report)
      self.adapter_results = adapter_results
      self.report          = report
    end

    def raw_headers
      @raw_headers ||= adapter_results.headers
    end

    def headers
      raise NotImplementedError.new("#{self.class.name} must implement `headers', use `raw_headers' for adapter headers")
    end

    def body
      size = rows.length - report.options[:footer].to_i
      @body ||= size < 0 ? [] : rows.first(size)
    end

    def footers
      @footer ||= rows.last(report.options[:footer].to_i)
    end

    def rows
      @rows ||= to_a
    end

    def arrays
      @arrays ||= [headers] + rows
    end

    def hashes
      return @hashes if defined?(@hashes)
      @hashes = rows.map { |row| row_hash(row) }
    end

    # this is the method that creates the individual hash entry
    # hashes should always use raw headers
    def row_hash(row)
      Hash[self.report.columns.map {|v| [v,row.send(v)]}]
    end

    def each
      raise NotImplementedError.new("#{self.class.name} must define `each`")
    end

    class Formatted < Result

      def headers
              @formatted_headers ||= self.report.columns.map { |h| report.format_header(h) }
      end

      def each
        adapter_results.rows.each { |row| yield format(row) }
      end

      def format(row)
       
        self.report.columns.each_with_index.map do |value, i|
          
          if row.is_a?(ActiveRecord::Base)
            column = value
            method = "format_#{column}"
          
            args = [column]
          #  puts "ARITY #{column}  #{row.method(column).arity}"
            if row.method(column).arity == -1            
               args << self.report.options
            end            
            value  = row.public_send(*args)
          else
            column = raw_headers.at(i)
            method = "format_#{column}"
          end

          if report.respond_to?(method)
{'fraction_non_alphanumeric': 0.10035478966041561, 'fraction_numerical': 0.0007602635580334517, 'mean_word_length': 2.017584097859327, 'pattern_counts': {'":': 0, '<': 9, '<?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': '880930', 'n_tokens_mistral': 1188, 'n_tokens_neox': 1156, 'n_words': 297}