<commit_before>require "delegate"
require "csv"

module Goldmine
  class HashRollup < SimpleDelegator
    def initialize(pivoted, cache=Cache.new)
      @cache = cache
      @context = RollupContext.new(@cache)
      @pivoted = pivoted
      super @rolled = {}
    end

    def rollup(name, &block)
      pivoted.each do |key, value|
        @cache.fetch(name, value) do
          rolled[key] ||= {}
          rolled[key][name] = @context.instance_exec(value, &block)
        end
      end
      self
    end

    def to_tabular
      [].tap do |rows|
        rows << tabular_header
        rows.concat tabular_rows
      end
    end

    def to_csv_table
      header = tabular_header
      rows = tabular_rows.map { |row| CSV::Row.new(header, row) }
      CSV::Table.new rows
    end

    private
<commit_msg>Expose rollup names<commit_after>require "delegate"
require "csv"

module Goldmine
  class HashRollup < SimpleDelegator
    attr_reader :names

    def initialize(pivoted, cache=Cache.new)
      @names = []
      @cache = cache
      @context = RollupContext.new(@cache)
      @pivoted = pivoted
      super @rolled = {}
    end

    def rollup(name, &block)
      names << name
      pivoted.each do |key, value|
        @cache.fetch(name, value) do
          rolled[key] ||= {}
          rolled[key][name] = @context.instance_exec(value, &block)
        end
      end
      self
    end

    def to_tabular
      [].tap do |rows|
        rows << tabular_header
        rows.concat tabular_rows
      end
    end

    def to_csv_table
      header = tabular_header
      rows = tabular_rows.map { |row| CSV::Row.new(header, row) }
      CSV::Table.new rows
    end

    private

{'fraction_non_alphanumeric': 0.10118343195266272, 'fraction_numerical': 0.0, 'mean_word_length': 2.154850746268657, 'pattern_counts': {'":': 0, '<': 11, '<?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': '4596813', 'n_tokens_mistral': 556, 'n_tokens_neox': 542, 'n_words': 133}