Sha256: b2a503fe6e040bf7ee28a50698fd83368498fe131bcb7aa58e65ec2796202a81

Contents?: true

Size: 1.75 KB

Versions: 20

Compression:

Stored size: 1.75 KB

Contents

module FactoryBot
  # @api private
  class StrategySyntaxMethodRegistrar
    def initialize(strategy_name)
      @strategy_name = strategy_name
    end

    def define_strategy_methods
      define_singular_strategy_method
      define_list_strategy_method
      define_pair_strategy_method
    end

    def self.with_index(block, index)
      if block&.arity == 2
        ->(instance) { block.call(instance, index) }
      else
        block
      end
    end

    private

    def define_singular_strategy_method
      strategy_name = @strategy_name

      define_syntax_method(strategy_name) do |name, *traits_and_overrides, &block|
        FactoryRunner.new(name, strategy_name, traits_and_overrides).run(&block)
      end
    end

    def define_list_strategy_method
      strategy_name = @strategy_name

      define_syntax_method("#{strategy_name}_list") do |name, amount, *traits_and_overrides, &block|
        unless amount.respond_to?(:times)
          raise ArgumentError, "count missing for #{strategy_name}_list"
        end

        Array.new(amount) do |i|
          block_with_index = StrategySyntaxMethodRegistrar.with_index(block, i)
          send(strategy_name, name, *traits_and_overrides, &block_with_index)
        end
      end
    end

    def define_pair_strategy_method
      strategy_name = @strategy_name

      define_syntax_method("#{strategy_name}_pair") do |name, *traits_and_overrides, &block|
        Array.new(2) { send(strategy_name, name, *traits_and_overrides, &block) }
      end
    end

    def define_syntax_method(name, &block)
      FactoryBot::Syntax::Methods.module_exec do
        if method_defined?(name) || private_method_defined?(name)
          undef_method(name)
        end

        define_method(name, &block)
      end
    end
  end
end

Version data entries

20 entries across 19 versions & 3 rubygems

Version Path
factory_bot-6.5.1 lib/factory_bot/strategy_syntax_method_registrar.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/factory_bot-6.5.0/lib/factory_bot/strategy_syntax_method_registrar.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/factory_bot-6.5.0/lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.5.0 lib/factory_bot/strategy_syntax_method_registrar.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/factory_bot-6.4.6/lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.4.6 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.4.5 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.4.4 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.4.3 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.4.2 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.4.1 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.4.0 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.3.0 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.2.1 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.2.0 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.1.0 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.0.2 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.0.1 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-6.0.0 lib/factory_bot/strategy_syntax_method_registrar.rb
factory_bot-5.2.0 lib/factory_bot/strategy_syntax_method_registrar.rb