2017-10-20 15:20:28 -04:00
|
|
|
module FactoryBot
|
2012-02-17 23:19:57 -05:00
|
|
|
class FactoryRunner
|
|
|
|
def initialize(name, strategy, traits_and_overrides)
|
2020-06-05 15:15:18 -04:00
|
|
|
@name = name
|
2012-02-17 23:19:57 -05:00
|
|
|
@strategy = strategy
|
|
|
|
|
2012-03-09 16:14:06 -05:00
|
|
|
@overrides = traits_and_overrides.extract_options!
|
2020-06-05 15:15:18 -04:00
|
|
|
@traits = traits_and_overrides
|
2012-02-17 23:19:57 -05:00
|
|
|
end
|
|
|
|
|
2012-04-25 15:29:02 -04:00
|
|
|
def run(runner_strategy = @strategy, &block)
|
2019-04-26 15:48:37 -04:00
|
|
|
factory = FactoryBot::Internal.factory_by_name(@name)
|
2012-02-17 23:19:57 -05:00
|
|
|
|
2012-03-09 14:05:05 -05:00
|
|
|
factory.compile
|
|
|
|
|
2012-02-17 23:19:57 -05:00
|
|
|
if @traits.any?
|
|
|
|
factory = factory.with_traits(@traits)
|
|
|
|
end
|
|
|
|
|
2015-05-19 23:18:34 -04:00
|
|
|
instrumentation_payload = {
|
|
|
|
name: @name,
|
|
|
|
strategy: runner_strategy,
|
|
|
|
traits: @traits,
|
|
|
|
overrides: @overrides,
|
2020-06-05 15:15:18 -04:00
|
|
|
factory: factory
|
2015-05-19 23:18:34 -04:00
|
|
|
}
|
2012-05-04 12:05:31 -04:00
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
ActiveSupport::Notifications.instrument("factory_bot.run_factory", instrumentation_payload) do
|
2012-05-04 12:05:31 -04:00
|
|
|
factory.run(runner_strategy, @overrides, &block)
|
|
|
|
end
|
2012-02-17 23:19:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|