<commit_before>require "rake"

require "magic_lamp/callbacks"

require "magic_lamp/configuration"
require "magic_lamp/fixture_creator"
require "magic_lamp/render_catcher"
require "magic_lamp/engine"
require "tasks/lint_task"
require "tasks/fixture_names_task"

module MagicLamp
  LAMP = "_lamp"
  SPEC = "spec"
  STARS = "**"
  TEST = "test"
  Genie = Engine

  class AmbiguousFixtureNameError < StandardError
  end

  class UnregisteredFixtureError < StandardError
  end

  class AlreadyRegisteredFixtureError < StandardError
  end

  class ArgumentError < StandardError
  end

  class << self
    attr_accessor :registered_fixtures, :configuration

    def path
      Rails.root.join(directory_path)
    end

    def register_fixture(options = {}, &block)
      controller_class = options.fetch(:controller, ::ApplicationController)
      fixture_name = options[:name]
      if block.nil?
        raise ArgumentError, "MagicLamp#register_fixture requires a block"
      end

      if fixture_name.nil? && configuration.infer_names
        fixture_name = default_fixture_name(controller_class, block)
      elsif fixture_name.nil?
        raise ArgumentError, "You must specify a name since `infer_names` is configured to `false`"
      end

      if registered?(fixture_name)
        raise AlreadyRegisteredFixtureError, "a fixture called '#{fixture_name}' has already been registered"
      end

      registered_fixtures[fixture_name] = [controller_class, block]
    end

    alias_method :rub, :register_fixture
    alias_method :wish, :register_fixture

    def configure(&block)
      if block.nil?
        raise ArgumentError, "MagicLamp#configure requires a block"
      end
      block.call(configuration)
    end

    def registered?(fixture_name)
      registered_fixtures.key?(fixture_name)
    end

    def load_config
      load_all(config_files)
    end

    def load_lamp_files
      self.registered_fixtures = {}
      load_config
      load_all(lamp_files)
    end

    def generate_fixture(fixture_name)
      unless registered?(fixture_name)
        raise UnregisteredFixtureError, "'#{fixture_name}' is not a registered fixture"
      end
      controller_class, block = registered_fixtures[fixture_name]
      FixtureCreator.new(configuration).generate_template(controller_class, &block)
    end

    def generate_all_fixtures
      load_lamp_files
      registered_fixtures.keys.each_with_object({}) do |fixture_name, fixtures|
        fixtures[fixture_name] = generate_fixture(fixture_name)
      end
    end

    private

    def config_files
      Dir[path.join(STARS, "magic#{LAMP}_config.rb")]
    end

    def lamp_files
      Dir[path.join(STARS, "*#{LAMP}.rb")]
    end

    def default_fixture_name(controller_class, block)
      first_arg = first_render_arg(block)<commit_msg>refactored missing block error stuff into private method<commit_after>require "rake"

require "magic_lamp/callbacks"

require "magic_lamp/configuration"
require "magic_lamp/fixture_creator"
require "magic_lamp/render_catcher"
require "magic_lamp/engine"
require "tasks/lint_task"
require "tasks/fixture_names_task"

module MagicLamp
  LAMP = "_lamp"
  SPEC = "spec"
  STARS = "**"
  TEST = "test"
  Genie = Engine

  class AmbiguousFixtureNameError < StandardError
  end

  class UnregisteredFixtureError < StandardError
  end

  class AlreadyRegisteredFixtureError < StandardError
  end

  class ArgumentError < StandardError
  end

  class << self
    attr_accessor :registered_fixtures, :configuration

    def path
      Rails.root.join(directory_path)
    end

    def register_fixture(options = {}, &block)
      controller_class = options.fetch(:controller, ::ApplicationController)
      fixture_name = options[:name]

      raise_missing_block_error(block, __method__)

      if fixture_name.nil? && configuration.infer_names
        fixture_name = default_fixture_name(controller_class, block)
      elsif fixture_name.nil?
        raise ArgumentError, "You must specify a name since `infer_names` is configured to `false`"
      end

      if registered?(fixture_name)
        raise AlreadyRegisteredFixtureError, "a fixture called '#{fixture_name}' has already been registered"
      end

      registered_fixtures[fixture_name] = [controller_class, block]
    end

    alias_method :rub, :register_fixture
    alias_method :wish, :register_fixture

    def configure(&block)
      raise_missing_block_error(block, __method__)
      block.call(configuration)
    end

    def registered?(fixture_name)
      registered_fixtures.key?(fixture_name)
    end

    def load_config
      load_all(config_files)
    end

    def load_lamp_files
      self.registered_fixtures = {}
      load_config
      load_all(lamp_files)
    end

    def generate_fixture(fixture_name)
      unless registered?(fixture_name)
        raise UnregisteredFixtureError, "'#{fixture_name}' is not a registered fixture"
      end
      controller_class, block = registered_fixtures[fixture_name]
      FixtureCreator.new(configuration).generate_template(controller_class, &block)
    end

    def generate_all_fixtures
      load_lamp_files
      registered_fixtures.keys.each_with_object({}) do |fixture_name, fixtures|
        fixtures[fixture_name] = generate_fixture(fixture_name)
      end
    end

    private

    def raise_missing_block_error(block, method_name)
      if block.nil?
        raise ArgumentError, "MagicLamp##{method_name} requires a block"
      end
    end

    def config_files
      Dir[path.join(STARS, "magic#{LAMP}_config.rb")]
    end

    def lamp_files
      Dir[path.join(STARS, "*#{LAMP}.rb")]
    end

    def default_fixture_name(controller_class, block)
      first_arg = first_render_arg(block)
{'fraction_non_alphanumeric': 0.07680223424681445, 'fraction_numerical': 0.0, 'mean_word_length': 3.6135265700483092, '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': '3171016', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1729, 'n_words': 379}