<commit_before>module FixtureToFactory
  class FactoryWriter
    def self.write_files(folder, file_hashes)
      file_hashes.each do |fixture_path, fixtures|
        write_fixtures_to_file(folder, fixture_path, fixtures)
      end
    end

    private

    class << self
      def write_fixtures_to_file(folder, fixture_path, fixtures)
        fixture_file_name = fixture_path.split('/').last.split('.')[0..-2].join('.')
        class_name = ActiveSupport::Inflector.singularize(fixture_file_name).capitalize
        factory_file_name = "generated_#{fixture_file_name}.rb"

        FileUtils.mkdir_p(folder)
        file = File.new("#{folder}/#{factory_file_name}", 'w')
        file.write(convert_yaml_hashes_to_factory_file(class_name, fixtures))
      end

      def convert_yaml_hashes_to_factory_file(class_name, fixture_yaml_hash)
        output = "FactoryGirl.define do\n"
        fixture_yaml_hash.each do |fixture, values|
          output += "\n  factory :#{fixture}, class: #{class_name}\n"
          values.each do |key, value|
            output += "    #{attribute_to_factory_line(key, value)}\n"
          end
          output += "  end\n"
        end
        output += "\nend"
      end

      def attribute_to_factory_line(key, value)
        result = ''
        if key != 'id'
          result = "#{key} { "
          result += value_in_factory_format(value)
          result += " }"
        end
        result
      end

      def value_in_factory_format(value)
        value_class = value.class
        case value_class.to_s
          when 'TrueClass'
            true
          when 'FalseClass'
            false
          when 'String'
            "%|#{value.gsub('|', '\|')}|"
          else
            "#{value}"
        end
      end
    end
  end
end
<commit_msg>Added a bit of output.<commit_after>module FixtureToFactory
  class FactoryWriter
    def self.write_files(folder, file_hashes)
      file_hashes.each do |fixture_path, fixtures|
        write_fixtures_to_file(folder, fixture_path, fixtures)
      end
    end

    private

    class << self
      def write_fixtures_to_file(folder, fixture_path, fixtures)
        fixture_file_name = fixture_path.split('/').last.split('.')[0..-2].join('.')
        class_name = ActiveSupport::Inflector.singularize(fixture_file_name).capitalize
        factory_file_name = "generated_#{fixture_file_name}.rb"

        puts "Writing #{fixture_file_name} fixtures to factory file #{factory_file_name}."

        FileUtils.mkdir_p(folder)
        file = File.new("#{folder}/#{factory_file_name}", 'w')
        file.write(convert_yaml_hashes_to_factory_file(class_name, fixtures))
      end

      def convert_yaml_hashes_to_factory_file(class_name, fixture_yaml_hash)
        output = "FactoryGirl.define do\n"
        fixture_yaml_hash.each do |fixture, values|
          output += "\n  factory :#{fixture}, class: #{class_name}\n"
          values.each do |key, value|
            output += "    #{attribute_to_factory_line(key, value)}\n"
          end
          output += "  end\n"
        end
        output += "\nend"
      end

      def attribute_to_factory_line(key, value)
        result = ''
        if key != 'id'
          result = "#{key} { "
          result += value_in_factory_format(value)
          result += " }"
        end
        result
      end

      def value_in_factory_format(value)
        value_class = value.class
        case value_class.to_s
          when 'TrueClass'
            true
          when 'FalseClass'
            false
          when 'String'
            "%|#{value.gsub('|', '\|')}|"
          else
            "#{value}"
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11023622047244094, 'fraction_numerical': 0.0010860711376595167, 'mean_word_length': 2.1758620689655173, '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': '4810545', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1136, 'n_words': 218}