Sha256: 5d31005628b903db23d10a7115b6ae0a5b714c80f83e91ff4486c66daef4cdb1

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

module FactoryBot
  module Syntax
    module Default
      include Methods

      def define(&block)
        DSL.run(block)
      end

      def modify(&block)
        ModifyDSL.run(block)
      end

      class DSL
        def factory(name, options = {}, &block)
          factory = Factory.new(name, options)
          proxy = FactoryBot::DefinitionProxy.new(factory.definition)
          proxy.instance_eval(&block) if block

          Internal.register_factory(factory)

          proxy.child_factories.each do |(child_name, child_options, child_block)|
            parent_factory = child_options.delete(:parent) || name
            factory(child_name, child_options.merge(parent: parent_factory), &child_block)
          end
        end

        def sequence(name, ...)
          Internal.register_sequence(Sequence.new(name, ...))
        end

        def trait(name, &block)
          Internal.register_trait(Trait.new(name, &block))
        end

        def self.run(block)
          new.instance_eval(&block)
        end

        delegate :after,
          :before,
          :callback,
          :initialize_with,
          :skip_create,
          :to_create,
          to: FactoryBot::Internal
      end

      class ModifyDSL
        def factory(name, _options = {}, &block)
          factory = Internal.factory_by_name(name)
          proxy = FactoryBot::DefinitionProxy.new(factory.definition.overridable)
          proxy.instance_eval(&block)
        end

        def self.run(block)
          new.instance_eval(&block)
        end
      end
    end
  end

  extend Syntax::Default
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
factory_bot-6.5.0 lib/factory_bot/syntax/default.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/factory_bot-6.4.6/lib/factory_bot/syntax/default.rb
factory_bot-6.4.6 lib/factory_bot/syntax/default.rb
factory_bot-6.4.5 lib/factory_bot/syntax/default.rb