1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/lib/factory_bot/syntax/default.rb

65 lines
1.6 KiB
Ruby
Raw Normal View History

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
2011-08-09 20:29:02 -04:00
Internal.register_factory(factory)
proxy.child_factories.each do |(child_name, child_options, child_block)|
parent_factory = child_options.delete(:parent) || name
2012-03-09 17:20:38 -05:00
factory(child_name, child_options.merge(parent: parent_factory), &child_block)
end
end
2011-08-12 10:35:41 -04:00
2012-04-06 14:41:13 -04:00
def sequence(name, *args, &block)
Internal.register_sequence(Sequence.new(name, *args, &block))
2012-03-29 10:21:55 -04:00
end
2011-08-12 16:16:17 -04:00
def trait(name, &block)
Internal.register_trait(Trait.new(name, &block))
2011-08-10 14:11:53 -04:00
end
def self.run(block)
new.instance_eval(&block)
end
delegate :after,
:before,
:callback,
:initialize_with,
:skip_create,
:to_create,
to: FactoryBot::Internal
2012-05-05 00:53:19 -04:00
end
2012-05-05 00:53:19 -04:00
class ModifyDSL
2018-10-05 14:54:08 -04:00
def factory(name, _options = {}, &block)
factory = Internal.factory_by_name(name)
proxy = FactoryBot::DefinitionProxy.new(factory.definition.overridable)
proxy.instance_eval(&block)
end
2012-05-05 00:53:19 -04:00
def self.run(block)
new.instance_eval(&block)
end
end
end
end
extend Syntax::Default
end