2018-07-16 18:31:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "factory_bot_rails/definition_file_paths"
|
|
|
|
|
|
|
|
module FactoryBotRails
|
|
|
|
class Reloader
|
2020-06-15 10:41:04 -04:00
|
|
|
def initialize(app)
|
2018-07-16 18:31:25 -04:00
|
|
|
@app = app
|
2019-04-04 01:24:07 -04:00
|
|
|
@paths = DefinitionFilePaths.new(FactoryBot.definition_file_paths)
|
2018-07-16 18:31:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2019-04-04 01:24:07 -04:00
|
|
|
return unless @paths.any?
|
|
|
|
|
2018-07-16 18:31:25 -04:00
|
|
|
register_reloader(build_reloader)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-06-15 10:41:04 -04:00
|
|
|
attr_reader :app
|
2018-07-16 18:31:25 -04:00
|
|
|
|
|
|
|
def build_reloader
|
2019-04-04 01:24:07 -04:00
|
|
|
reloader_class.new(@paths.files, @paths.directories) do
|
2018-07-16 18:31:25 -04:00
|
|
|
FactoryBot.reload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reloader_class
|
|
|
|
app.config.file_watcher
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_reloader(reloader)
|
2020-06-15 10:41:04 -04:00
|
|
|
app.reloader.to_prepare do
|
|
|
|
reloader.execute
|
2018-07-16 18:31:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
app.reloaders << reloader
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|