Skip to content

Commit 213bd29

Browse files
create AttributeSequence class
1 parent b593ce1 commit 213bd29

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

lib/factory_bot.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
require "factory_bot/decorator/new_constructor"
4848
require "factory_bot/linter"
4949
require "factory_bot/version"
50+
require "factory_bot/attribute_sequence"
5051

5152
module FactoryBot
5253
Deprecation = ActiveSupport::Deprecation.new("7.0", "factory_bot")

lib/factory_bot/attribute_sequence.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module FactoryBot
2+
# @api private
3+
class AttributeSequence
4+
attr_reader :expression
5+
6+
def initialize(&block)
7+
@expression = block
8+
end
9+
10+
def evaluate(index)
11+
expression.call(index)
12+
end
13+
14+
def self.evaluate_attributes(traits_and_overrides, i)
15+
traits_and_overrides.map do |attribute|
16+
next attribute unless attribute.is_a?(Hash)
17+
18+
attribute.transform_values do |value|
19+
value.is_a?(self) ? value.evaluate(i) : value
20+
end
21+
end
22+
end
23+
end
24+
end

lib/factory_bot/strategy_syntax_method_registrar.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def define_list_strategy_method
3939

4040
Array.new(amount) do |i|
4141
block_with_index = StrategySyntaxMethodRegistrar.with_index(block, i)
42-
send(strategy_name, name, *evaluate_sequence_attributes(traits_and_overrides, i), &block_with_index)
42+
traits_and_evaluated_overrides =
43+
FactoryBot::AttributeSequence.evaluate_attributes(traits_and_overrides, i)
44+
send(strategy_name, name, *traits_and_evaluated_overrides, &block_with_index)
4345
end
4446
end
4547
end

lib/factory_bot/syntax/methods.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,7 @@ def generate_list(name, count)
131131
end
132132

133133
def build_sequence(&block)
134-
block
135-
end
136-
137-
def evaluate_sequence_attributes(traits_and_overrides, i)
138-
traits_and_overrides.map do |attribute|
139-
next attribute unless attribute.is_a?(Hash)
140-
141-
attribute.transform_values { |value| value.respond_to?(:call) ? value.call(i) : value }
142-
end
134+
FactoryBot::AttributeSequence.new(&block)
143135
end
144136
end
145137
end

spec/acceptance/create_list_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
end
4040

4141
context "with sequencial attributes" do
42-
subject { FactoryBot.create_list(:post, 20, title: FactoryBot.build_sequence{|n| "title_#{n}"}) }
42+
subject { FactoryBot.create_list(:post, 20, title: FactoryBot.build_sequence{ |n| "title_#{n}" }) }
4343

4444
it "create sequencial attribute values" do
4545
subject.each_with_index do |record, i|

0 commit comments

Comments
 (0)