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
Daniel Colson d2a30d6fd2 Introduce FactoryBot::Internal module
The `FactoryBot` module has a mixture of methods that are meant for use
by people using the library, and methods that are meant only for
internal use. The methods meant for internal use are cluttering the
documentation, and may be confusing to users.

This change was prompted by [#1258]. Rather than introduce yet another
public method on `FactoryBot` meant only for internal use,
we can introduce a `FactoryBot::Internal` module,
and avoid generating documentation for that module.

The `FactoryBot::Internal.register_inline_sequence` method in [#1258]
will need access to the configuration instance, so this PR moves that
into `FactoryBot::Internal`. Eventually I plan to deprecate
`FactoryBot.configuration` and `FactoryBot.reset_configuration`, and to
move more of the `FactoryBot` methods into `FactoryBot::Internal`, but I
would rather hold off on all that until the dust settles on the 5.0
release.

[#1258]: https://github.com/thoughtbot/factory_bot/pull/1258
2019-02-13 11:31:49 -05:00

76 lines
1.8 KiB
Ruby

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_given?
FactoryBot.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, *args, &block)
FactoryBot.register_sequence(Sequence.new(name, *args, &block))
end
def trait(name, &block)
FactoryBot.register_trait(Trait.new(name, &block))
end
def to_create(&block)
FactoryBot.to_create(&block)
end
def skip_create
FactoryBot.skip_create
end
def initialize_with(&block)
FactoryBot.initialize_with(&block)
end
def self.run(block)
new.instance_eval(&block)
end
delegate :before, :after, :callback, to: :configuration
private
def configuration
FactoryBot::Internal.configuration
end
end
class ModifyDSL
def factory(name, _options = {}, &block)
factory = FactoryBot.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