Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/factory_bot/internal/strategies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ def self.reset
@strategies&.reset
end

def self.register_strategy(strategy_name, strategy_class)
strategies.register(strategy_name, strategy_class)
StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods
end

def self.strategy_by_name(name)
strategies.find(name)
end
Expand All @@ -25,6 +20,11 @@ def self.register_default_strategies
register_strategy(:build_stubbed, FactoryBot::Strategy::Stub)
register_strategy(:null, FactoryBot::Strategy::Null)
end

def self.register_strategy(strategy_name, strategy_class)
strategies.register(strategy_name, strategy_class)
StrategySyntaxMethodRegistrar.define_strategy_methods(strategy_name)
end
end
end
end
50 changes: 21 additions & 29 deletions lib/factory_bot/strategy_syntax_method_registrar.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
module FactoryBot
# @api private
class StrategySyntaxMethodRegistrar
def initialize(strategy_name)
@strategy_name = strategy_name
module StrategySyntaxMethodRegistrar
def self.define_strategy_methods(strategy_name)
define_singular_strategy_method(strategy_name)
define_list_strategy_method(strategy_name)
define_pair_strategy_method(strategy_name)
end
Comment on lines +4 to 8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, I think I prefer the object oriented approach given how how strategy_name is passed around. I'd consider defining the define_strategy_methods class method as new(strategy_name).define_strategy_methods


def define_strategy_methods
define_singular_strategy_method
define_list_strategy_method
define_pair_strategy_method
end

def self.with_index(block, index)
if block&.arity == 2
->(instance) { block.call(instance, index) }
else
block
end
end

private

def define_singular_strategy_method
strategy_name = @strategy_name

def self.define_singular_strategy_method(strategy_name)
define_syntax_method(strategy_name) do |name, *traits_and_overrides, &block|
FactoryRunner.new(name, strategy_name, traits_and_overrides).run(&block)
end
end
private_class_method :define_singular_strategy_method

def define_list_strategy_method
strategy_name = @strategy_name

def self.define_list_strategy_method(strategy_name)
define_syntax_method("#{strategy_name}_list") do |name, amount, *traits_and_overrides, &block|
unless amount.respond_to?(:times)
raise ArgumentError, "count missing for #{strategy_name}_list"
Expand All @@ -43,16 +26,16 @@ def define_list_strategy_method
end
end
end
private_class_method :define_list_strategy_method

def define_pair_strategy_method
strategy_name = @strategy_name

def self.define_pair_strategy_method(strategy_name)
define_syntax_method("#{strategy_name}_pair") do |name, *traits_and_overrides, &block|
Array.new(2) { send(strategy_name, name, *traits_and_overrides, &block) }
end
end
private_class_method :define_pair_strategy_method

def define_syntax_method(name, &block)
def self.define_syntax_method(name, &block)
FactoryBot::Syntax::Methods.module_exec do
if method_defined?(name) || private_method_defined?(name)
undef_method(name)
Expand All @@ -61,5 +44,14 @@ def define_syntax_method(name, &block)
define_method(name, &block)
end
end
private_class_method :define_syntax_method

def self.with_index(block, index)
if block&.arity == 2
->(instance) { block.call(instance, index) }
else
block
end
end
end
end