Skip to content

Commit b8e9a0a

Browse files
authored
Refactor factory calculator (#1770)
* refactor: rewrite FactoryCalculator to be functional * refactor: rename strategy_calculator.rb to strategy.rb * refactor: move strategy namespace require statements
1 parent d12d437 commit b8e9a0a

File tree

5 files changed

+24
-41
lines changed

5 files changed

+24
-41
lines changed

lib/factory_bot.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@
1111
require "factory_bot/errors"
1212
require "factory_bot/factory_runner"
1313
require "factory_bot/strategy_syntax_method_registrar"
14-
require "factory_bot/strategy_calculator"
15-
require "factory_bot/strategy/build"
16-
require "factory_bot/strategy/create"
17-
require "factory_bot/strategy/attributes_for"
18-
require "factory_bot/strategy/stub"
19-
require "factory_bot/strategy/null"
14+
require "factory_bot/strategy"
2015
require "factory_bot/registry"
2116
require "factory_bot/null_factory"
2217
require "factory_bot/null_object"

lib/factory_bot/factory.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ def run(build_strategy, overrides, &block)
3636

3737
compile
3838

39-
strategy = StrategyCalculator.new(build_strategy).strategy.new
39+
strategy = Strategy.lookup_strategy(build_strategy).new
4040

4141
evaluator = evaluator_class.new(strategy, overrides.symbolize_keys)
4242
attribute_assigner = AttributeAssigner.new(evaluator, build_class, &compiled_constructor)
4343

4444
observer = CallbacksObserver.new(callbacks, evaluator)
45-
evaluation =
46-
Evaluation.new(evaluator, attribute_assigner, compiled_to_create, observer)
45+
evaluation = Evaluation.new(evaluator, attribute_assigner, compiled_to_create, observer)
4746

4847
evaluation.notify(:before_all, nil)
4948
instance = strategy.result(evaluation).tap(&block)

lib/factory_bot/strategy.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require "factory_bot/strategy/build"
2+
require "factory_bot/strategy/create"
3+
require "factory_bot/strategy/attributes_for"
4+
require "factory_bot/strategy/stub"
5+
require "factory_bot/strategy/null"
6+
7+
module FactoryBot
8+
module Strategy
9+
def self.lookup_strategy(name_or_object)
10+
return name_or_object if name_or_object.is_a?(Class)
11+
12+
FactoryBot::Internal.strategy_by_name(name_or_object)
13+
end
14+
end
15+
end

lib/factory_bot/strategy_calculator.rb

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
describe FactoryBot::StrategyCalculator do
1+
describe FactoryBot::Strategy do
22
it "returns the class passed when it is instantiated with a class" do
33
strategy = define_class("MyAwesomeClass")
4-
calculator = FactoryBot::StrategyCalculator.new(strategy).strategy
4+
result = described_class.lookup_strategy(strategy)
55

6-
expect(calculator).to eq strategy
6+
expect(result).to eq strategy
77
end
88

99
it "finds the strategy by name when instantiated with a symbol" do
1010
strategy = define_class("MyAwesomeClass")
1111
allow(FactoryBot::Internal).to receive(:strategy_by_name).and_return(strategy)
12-
FactoryBot::StrategyCalculator.new(:build).strategy
12+
described_class.lookup_strategy(:build)
1313

1414
expect(FactoryBot::Internal).to have_received(:strategy_by_name).with(:build)
1515
end
1616

1717
it "returns the strategy found when instantiated with a symbol" do
1818
strategy = define_class("MyAwesomeClass")
1919
allow(FactoryBot::Internal).to receive(:strategy_by_name).and_return(strategy)
20-
calculator = FactoryBot::StrategyCalculator.new(:build).strategy
20+
result = described_class.lookup_strategy(:build)
2121

22-
expect(calculator).to eq strategy
22+
expect(result).to eq strategy
2323
end
2424
end

0 commit comments

Comments
 (0)