2017-10-20 19:20:28 +00:00
|
|
|
module FactoryBot
|
2012-02-18 04:19:57 +00:00
|
|
|
class FactoryRunner
|
|
|
|
def initialize(name, strategy, traits_and_overrides)
|
|
|
|
@name = name
|
|
|
|
@strategy = strategy
|
|
|
|
|
2012-03-09 21:14:06 +00:00
|
|
|
@overrides = traits_and_overrides.extract_options!
|
2018-07-28 14:55:24 +00:00
|
|
|
@traits = traits_and_overrides.map(&:to_sym)
|
2012-02-18 04:19:57 +00:00
|
|
|
end
|
|
|
|
|
2012-04-25 19:29:02 +00:00
|
|
|
def run(runner_strategy = @strategy, &block)
|
2017-10-20 19:20:28 +00:00
|
|
|
factory = FactoryBot.factory_by_name(@name)
|
2012-02-18 04:19:57 +00:00
|
|
|
|
2012-03-09 19:05:05 +00:00
|
|
|
factory.compile
|
|
|
|
|
2012-02-18 04:19:57 +00:00
|
|
|
if @traits.any?
|
|
|
|
factory = factory.with_traits(@traits)
|
|
|
|
end
|
|
|
|
|
2015-05-20 03:18:34 +00:00
|
|
|
instrumentation_payload = {
|
|
|
|
name: @name,
|
|
|
|
strategy: runner_strategy,
|
|
|
|
traits: @traits,
|
|
|
|
overrides: @overrides,
|
2018-10-07 22:02:54 +00:00
|
|
|
factory: factory,
|
2015-05-20 03:18:34 +00:00
|
|
|
}
|
2012-05-04 16:05:31 +00:00
|
|
|
|
2018-10-08 01:45:51 +00:00
|
|
|
ActiveSupport::Notifications.instrument("factory_bot.run_factory", instrumentation_payload) do
|
2012-05-04 16:05:31 +00:00
|
|
|
factory.run(runner_strategy, @overrides, &block)
|
|
|
|
end
|
2012-02-18 04:19:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|