Sha256: e77ea62faef575fd055727ea32ab83fd948f184d220906d9957c942c1d9845d1

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module FactoryBot
  class With
    # A <code>FactoryBot::Syntax::Methods</code> replacement to enable <code>factory_bot-with</code> features.
    module Methods
      include FactoryBot::Syntax::Methods

      BUILD_STRATEGIES = %i[build build_stubbed create attributes_for with].freeze
      VARIATIONS = {
        unit: BUILD_STRATEGIES.to_h { [_1, _1] }.freeze,
        pair: BUILD_STRATEGIES.to_h { [_1, :"#{_1}_pair"] }.freeze,
        list: BUILD_STRATEGIES.to_h { [_1, :"#{_1}_list"] }.freeze,
      }.freeze

      VARIATIONS.each do |variation, build_strategies|
        build_strategies.each do |build_strategy, method_name|
          define_method(method_name) do |factory = nil, *args, **kwargs, &block|
            return Proxy.new(self, __method__) unless factory

            with =
              if factory.is_a? With
                factory.merge(With.new(variation, factory.factory_name, *args, **kwargs, &block))
              else
                With.new(variation, factory, *args, **kwargs, &block)
              end

            with.instantiate(build_strategy)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
factory_bot-with-0.1.0 lib/factory_bot/with/methods.rb