From 242f8dc6660fd9948ac5e2f1c62e53e4fee1dda0 Mon Sep 17 00:00:00 2001 From: Valerie Burzynski Date: Tue, 30 Sep 2025 18:29:35 -0500 Subject: [PATCH] refactor: make EvaluatorClassDefiner functional The work performed in FactoryBot::EvaluatorClassDefiner can be done without creating an instance of the class. It can be rewritten in a more functional manner with a module. --- lib/factory_bot/evaluator_class_definer.rb | 20 +++++++------------ lib/factory_bot/factory.rb | 2 +- .../evaluator_class_definer_spec.rb | 3 +-- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/factory_bot/evaluator_class_definer.rb b/lib/factory_bot/evaluator_class_definer.rb index 332539580..92d5ae855 100644 --- a/lib/factory_bot/evaluator_class_definer.rb +++ b/lib/factory_bot/evaluator_class_definer.rb @@ -1,19 +1,13 @@ module FactoryBot # @api private - class EvaluatorClassDefiner - def initialize(attributes, parent_class) - @parent_class = parent_class - @attributes = attributes - - attributes.each do |attribute| - evaluator_class.define_attribute(attribute.name, &attribute.to_proc) - end - end - - def evaluator_class - @evaluator_class ||= Class.new(@parent_class).tap do |klass| + module EvaluatorClassDefiner + def self.define_evaluator_class(attributes, parent_class) + Class.new(parent_class).tap do |klass| klass.attribute_lists ||= [] - klass.attribute_lists += [@attributes] + klass.attribute_lists += [attributes] + attributes.each do |attribute| + klass.define_attribute(attribute.name, &attribute.to_proc) + end end end end diff --git a/lib/factory_bot/factory.rb b/lib/factory_bot/factory.rb index 3e34107c0..c33f57b1d 100644 --- a/lib/factory_bot/factory.rb +++ b/lib/factory_bot/factory.rb @@ -111,7 +111,7 @@ def class_name end def evaluator_class - @evaluator_class ||= EvaluatorClassDefiner.new(attributes, parent.evaluator_class).evaluator_class + @evaluator_class ||= EvaluatorClassDefiner.define_evaluator_class(attributes, parent.evaluator_class) end def attributes diff --git a/spec/factory_bot/evaluator_class_definer_spec.rb b/spec/factory_bot/evaluator_class_definer_spec.rb index e436f2487..ba493b2ab 100644 --- a/spec/factory_bot/evaluator_class_definer_spec.rb +++ b/spec/factory_bot/evaluator_class_definer_spec.rb @@ -60,11 +60,10 @@ def define_evaluator(arguments = {}) end def define_evaluator_class(arguments = {}) - evaluator_class_definer = FactoryBot::EvaluatorClassDefiner.new( + FactoryBot::EvaluatorClassDefiner.define_evaluator_class( arguments[:attributes] || [], arguments[:parent_class] || FactoryBot::Evaluator ) - evaluator_class_definer.evaluator_class end def stub_attribute(name = :attribute, &value)