2017-10-20 15:20:28 -04:00
|
|
|
module FactoryBot
|
2010-06-24 09:45:57 -04:00
|
|
|
module Syntax
|
|
|
|
module Default
|
2011-01-19 19:47:49 -05:00
|
|
|
include Methods
|
|
|
|
|
2010-07-06 20:54:55 -04:00
|
|
|
def define(&block)
|
|
|
|
DSL.run(block)
|
|
|
|
end
|
|
|
|
|
2011-09-02 12:05:00 -04:00
|
|
|
def modify(&block)
|
|
|
|
ModifyDSL.run(block)
|
|
|
|
end
|
|
|
|
|
2010-07-06 20:54:55 -04:00
|
|
|
class DSL
|
|
|
|
def factory(name, options = {}, &block)
|
|
|
|
factory = Factory.new(name, options)
|
2017-10-20 15:20:28 -04:00
|
|
|
proxy = FactoryBot::DefinitionProxy.new(factory.definition)
|
2021-02-20 16:55:25 -05:00
|
|
|
proxy.instance_eval(&block) if block
|
2011-08-09 20:29:02 -04:00
|
|
|
|
2019-04-26 15:48:37 -04:00
|
|
|
Internal.register_factory(factory)
|
2011-06-29 16:49:45 -04:00
|
|
|
|
|
|
|
proxy.child_factories.each do |(child_name, child_options, child_block)|
|
2012-01-23 00:51:51 -05:00
|
|
|
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)
|
2011-06-29 16:49:45 -04:00
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
|
2012-04-06 14:41:13 -04:00
|
|
|
def sequence(name, *args, &block)
|
2019-04-26 15:48:37 -04:00
|
|
|
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)
|
2019-04-19 16:01:32 -04:00
|
|
|
Internal.register_trait(Trait.new(name, &block))
|
2011-08-10 14:11:53 -04:00
|
|
|
end
|
2011-09-02 12:05:00 -04:00
|
|
|
|
|
|
|
def self.run(block)
|
|
|
|
new.instance_eval(&block)
|
|
|
|
end
|
2013-02-08 11:00:22 -05:00
|
|
|
|
2020-04-23 19:14:02 -04:00
|
|
|
delegate :after,
|
2020-06-05 15:15:18 -04:00
|
|
|
:before,
|
|
|
|
:callback,
|
|
|
|
:initialize_with,
|
|
|
|
:skip_create,
|
|
|
|
:to_create,
|
|
|
|
to: FactoryBot::Internal
|
2012-05-05 00:53:19 -04:00
|
|
|
end
|
2011-09-02 12:05:00 -04:00
|
|
|
|
2012-05-05 00:53:19 -04:00
|
|
|
class ModifyDSL
|
2018-10-05 14:54:08 -04:00
|
|
|
def factory(name, _options = {}, &block)
|
2019-04-26 15:48:37 -04:00
|
|
|
factory = Internal.factory_by_name(name)
|
2017-10-20 15:20:28 -04:00
|
|
|
proxy = FactoryBot::DefinitionProxy.new(factory.definition.overridable)
|
2011-09-02 12:05:00 -04:00
|
|
|
proxy.instance_eval(&block)
|
|
|
|
end
|
2012-05-05 00:53:19 -04:00
|
|
|
|
|
|
|
def self.run(block)
|
|
|
|
new.instance_eval(&block)
|
|
|
|
end
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-11 15:00:14 -04:00
|
|
|
|
2010-07-06 20:54:55 -04:00
|
|
|
extend Syntax::Default
|
|
|
|
end
|