<commit_before>require 'whisperer/version'

require 'virtus'
require 'vcr'

require 'whisperer/placeholder'
require 'whisperer/dsl'
require 'whisperer/helpers'

require 'whisperer/convertors/hash'

require 'whisperer/serializers/json'
require 'whisperer/serializers/json_multiple'

require 'whisperer/preprocessors/content_length'

module Whisperer
  @fixture_records = ThreadSafe::Hash.new
  @serializers     = ThreadSafe::Hash.new
  @preprocessors   = ThreadSafe::Hash.new

  class << self
    attr_reader :fixture_records
    attr_reader :serializers
    attr_reader :preprocessors

    def define(name, options = {}, &block)
      dsl = Dsl.build
      dsl.instance_eval &block
      record = dsl.container

      if options[:parent]
        original_record = fixture_records[options[:parent]]

        if original_record.nil?
          raise ArgumentError.new("Parent record \"#{options[:parent]}\" is not declired.")
        else
          record.merge!(original_record)
        end
      end

      fixture_records[name.to_sym] = record
    end

    # Returns true if at least one factory is defined, otherwise returns false.
    def defined_any?
      fixture_records.size > 0
    end

    def generate(name)
      name = name.to_sym

      unless fixture_records[name]
        raise NoFixtureRecordError.new("There is not fixture builder with \"#{name}\" name.")
      end

      container = fixture_records[name]

      preprocessors.each do |name, class_names|
        class_names.process(container)
      end

      hash = Whisperer::Convertors::Hash.convert(container)

      interaction = VCR::HTTPInteraction.from_hash(
        hash
      )

      cassette = VCR::Cassette.new("#{container.sub_path}/#{name}")
      cassette.record_http_interaction(
        interaction
      )

      cassette.eject

      File.read(
        "#{VCR.configuration.cassette_library_dir}/#{container.sub_path}/#{name}.yml"
      )
    end

    def generate_all
      if defined_any?
        fixture_records.each do |name, container|
          generate(name)
        end
      else
        raise NoFixtureRecordError.new('Fixture builders are not found.')
      end
    end

    def register_preprocessor(name, class_name)
      preprocessors[name] = class_name
    end

    def register_serializer(name, class_name)<commit_msg>add code to remove old fixture before generating a new one<commit_after>require 'whisperer/version'

require 'virtus'
require 'vcr'

require 'whisperer/placeholder'
require 'whisperer/dsl'
require 'whisperer/helpers'

require 'whisperer/convertors/hash'

require 'whisperer/serializers/json'
require 'whisperer/serializers/json_multiple'

require 'whisperer/preprocessors/content_length'

module Whisperer
  @fixture_records = ThreadSafe::Hash.new
  @serializers     = ThreadSafe::Hash.new
  @preprocessors   = ThreadSafe::Hash.new

  class << self
    attr_reader :fixture_records
    attr_reader :serializers
    attr_reader :preprocessors

    def define(name, options = {}, &block)
      dsl = Dsl.build
      dsl.instance_eval &block
      record = dsl.container

      if options[:parent]
        original_record = fixture_records[options[:parent]]

        if original_record.nil?
          raise ArgumentError.new("Parent record \"#{options[:parent]}\" is not declired.")
        else
          record.merge!(original_record)
        end
      end

      fixture_records[name.to_sym] = record
    end

    # Returns true if at least one factory is defined, otherwise returns false.
    def defined_any?
      fixture_records.size > 0
    end

    def generate(name)
      name = name.to_sym

      unless fixture_records[name]
        raise NoFixtureRecordError.new("There is not fixture builder with \"#{name}\" name.")
      end

      container = fixture_records[name]

      preprocessors.each do |name, class_names|
        class_names.process(container)
      end

      hash = Whisperer::Convertors::Hash.convert(container)

      interaction = VCR::HTTPInteraction.from_hash(
        hash
      )

      path_to_fixture = "#{VCR.configuration.cassette_library_dir}/#{container.sub_path}/#{name}.yml"

      if File.exists?(path_to_fixture)
        File.unlink(
          path_to_fixture
        )
      end

      cassette = VCR::Cassette.new("#{container.sub_path}/#{name}")
      cassette.record_http_interaction(
        interaction
      )

      cassette.eject

      File.read(path_to_fixture)
    end

    def generate_all
      if defined_any?
        fixture_records.each do |name, container|
          generate(name)
        end
      else
        raise NoFixtureRecordError.new('Fixture builders are not found.')
      end
    end

    def register_preprocessor(name, class_name)
      preprocessors[name] = class_name
    end

    def register_serializer(name, class_name)
{'fraction_non_alphanumeric': 0.09081484553182666, 'fraction_numerical': 0.00041467965996267884, 'mean_word_length': 2.8592, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '4392414', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1467, 'n_words': 311}