Module: Polyrun::Data::FactoryInstrumentation

Defined in:
lib/polyrun/data/factory_instrumentation.rb

Overview

Opt-in FactoryBot hook so #FactoryCounts sees every factory run (minimal patch). Requires the factory_bot gem and must run after FactoryBot is loaded.

require "factory_bot"
Polyrun::Data::FactoryInstrumentation.instrument_factory_bot!

Class Method Summary collapse

Class Method Details

.instrument_factory_bot!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/polyrun/data/factory_instrumentation.rb', line 10

def instrument_factory_bot!
  return false unless defined?(FactoryBot)

  factory_class = resolve_factory_runner_class
  return false unless factory_class

  return true if factory_class.instance_variable_defined?(:@polyrun_factory_instrumented) &&
    factory_class.instance_variable_get(:@polyrun_factory_instrumented)

  patch = Module.new do
    def run(...)
      Polyrun::Data::FactoryCounts.record(name)
      super
    end
  end
  factory_class.prepend(patch)
  factory_class.instance_variable_set(:@polyrun_factory_instrumented, true)
  true
end

.instrumented?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
# File 'lib/polyrun/data/factory_instrumentation.rb', line 30

def instrumented?
  return false unless defined?(FactoryBot)

  fc = resolve_factory_runner_class
  return false unless fc

  fc.instance_variable_defined?(:@polyrun_factory_instrumented) &&
    fc.instance_variable_get(:@polyrun_factory_instrumented)
end