<commit_before>module Dumpable
  class Dumper
    attr_accessor :dumpee, :options, :id_padding, :dumps

    def initialize(dumpee, options={})
      @dumpee = dumpee
      @options = Dumpable.config.merge(options || {})
      @id_padding = @options[:id_padding] || (@dumpee.class.respond_to?(:dumpable_options) && @dumpee.class.dumpable_options[:id_padding]) || Dumpable.config.id_padding
      @dumps = @options[:dumps] || (@dumpee.class.respond_to?(:dumpable_options) && @dumpee.class.dumpable_options[:dumps])
      @lines = []
    end

    def dump
      recursive_dump(@dumpee, @dumps)
      @lines << generate_insert_query(@dumpee)
    end

    def self.dump(*records_and_collections)
      options = records_and_collections.extract_options!
      lines = []
      records_and_collections.each do |record_or_collection|
        if record_or_collection.is_a?(Array) || record_or_collection.is_a?(ActiveRecord::Relation) || (record_or_collection.is_a?(Class) && record_or_collection.ancestors.include?(ActiveRecord::Base))
          record_or_collection = record_or_collection.all if record_or_collection.is_a?(Class) && record_or_collection.ancestors.include?(ActiveRecord::Base)
          record_or_collection.each do |object|
            lines << new(object, options).dump
          end
        else
          lines << new(record_or_collection, options).dump
        end
      end
      Dumpable::FileWriter.write(lines.flatten.compact, options)
    end

    private
    def recursive_dump(object, dumps)
      if dumps.nil?

      elsif dumps.is_a?(Array)
        dumps.each do |mini_dump|
          recursive_dump(object, mini_dump)
        end
      elsif dumps.is_a?(Hash)
        dumps.each do |key, value|
          recursive_dump(object, key)
          Array(object.send(key)).each { |child| recursive_dump(child, value) }
        end
      elsif dumps.is_a?(Symbol) || dumps.is_a?(String)
        Array(object.send(dumps)).each do |child_object|
          reflection = object.class.reflections[dumps.to_sym]
          if reflection.macro == :belongs_to
            object.send("#{reflection.association_foreign_key}=", object.id + @id_padding)
          elsif [:has_many, :has_one].include? reflection.macro
            if reflection.respond_to?(:foreign_key)
              child_object.send("#{reflection.foreign_key}=", object.id + @id_padding)<commit_msg>reflections uses string keys<commit_after>module Dumpable
  class Dumper
    attr_accessor :dumpee, :options, :id_padding, :dumps

    def initialize(dumpee, options={})
      @dumpee = dumpee
      @options = Dumpable.config.merge(options || {})
      @id_padding = @options[:id_padding] || (@dumpee.class.respond_to?(:dumpable_options) && @dumpee.class.dumpable_options[:id_padding]) || Dumpable.config.id_padding
      @dumps = @options[:dumps] || (@dumpee.class.respond_to?(:dumpable_options) && @dumpee.class.dumpable_options[:dumps])
      @lines = []
    end

    def dump
      recursive_dump(@dumpee, @dumps)
      @lines << generate_insert_query(@dumpee)
    end

    def self.dump(*records_and_collections)
      options = records_and_collections.extract_options!
      lines = []
      records_and_collections.each do |record_or_collection|
        if record_or_collection.is_a?(Array) || record_or_collection.is_a?(ActiveRecord::Relation) || (record_or_collection.is_a?(Class) && record_or_collection.ancestors.include?(ActiveRecord::Base))
          record_or_collection = record_or_collection.all if record_or_collection.is_a?(Class) && record_or_collection.ancestors.include?(ActiveRecord::Base)
          record_or_collection.each do |object|
            lines << new(object, options).dump
          end
        else
          lines << new(record_or_collection, options).dump
        end
      end
      Dumpable::FileWriter.write(lines.flatten.compact, options)
    end

    private
    def recursive_dump(object, dumps)
      if dumps.nil?

      elsif dumps.is_a?(Array)
        dumps.each do |mini_dump|
          recursive_dump(object, mini_dump)
        end
      elsif dumps.is_a?(Hash)
        dumps.each do |key, value|
          recursive_dump(object, key)
          Array(object.send(key)).each { |child| recursive_dump(child, value) }
        end
      elsif dumps.is_a?(Symbol) || dumps.is_a?(String)
        Array(object.send(dumps)).each do |child_object|
          reflection = object.class.reflections[dumps.to_s]
          if reflection.macro == :belongs_to
            object.send("#{reflection.association_foreign_key}=", object.id + @id_padding)
          elsif [:has_many, :has_one].include? reflection.macro
            if reflection.respond_to?(:foreign_key)
              child_object.send("#{reflection.foreign_key}=", object.id + @id_padding)
{'fraction_non_alphanumeric': 0.11977306156755621, 'fraction_numerical': 0.0, 'mean_word_length': 3.2728904847396767, 'pattern_counts': {'":': 0, '<': 15, '<?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': '2934925', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1527, 'n_words': 238}