Skip to content

Commit ad803ca

Browse files
committed
Refactor DSL
1 parent e8a1600 commit ad803ca

File tree

2 files changed

+61
-35
lines changed

2 files changed

+61
-35
lines changed

lib/rom/factory/dsl.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,15 @@ def call
7777
# @param [Symbol] The name of the registered builder
7878
#
7979
# @api public
80-
def create(name, *args)
81-
_factories[name, *args]
82-
end
80+
def create(name, *args) = _factories[name, *args]
8381

8482
# Create a sequence attribute
8583
#
8684
# @param [Symbol] name The attribute name
8785
#
8886
# @api private
89-
def sequence(meth, &block)
90-
define_sequence(meth, block) if _valid_names.include?(meth)
87+
def sequence(meth, &)
88+
define_sequence(meth, &) if _valid_names.include?(meth)
9189
end
9290

9391
# Set timestamp attributes
@@ -169,18 +167,22 @@ def trait(name, parents = [], &)
169167
# @option options [Array<Symbol>] traits Traits to apply to the association
170168
#
171169
# @api public
172-
def association(name, *traits, **options)
170+
def association(name, *seq_traits, traits: EMPTY_ARRAY, **options)
173171
assoc = _relation.associations[name]
174172

175173
if assoc.is_a?(::ROM::SQL::Associations::OneToOne) && options.fetch(:count, 1) > 1
176174
::Kernel.raise ::ArgumentError, "count cannot be greater than 1 on a OneToOne"
177175
end
178176

179-
traits = options.fetch(:traits, EMPTY_ARRAY) if traits.empty?
180-
181177
builder = -> { _factories.for_relation(assoc.target) }
182178

183-
_attributes << attributes::Association.new(assoc, builder, *traits, **options)
179+
_attributes << attributes::Association.new(
180+
assoc,
181+
builder,
182+
*seq_traits,
183+
*traits,
184+
**options
185+
)
184186
end
185187

186188
# @api private
@@ -204,8 +206,8 @@ def respond_to_missing?(method_name, include_private = false)
204206
end
205207

206208
# @api private
207-
def define_sequence(name, block)
208-
_attributes << attributes::Callable.new(name, self, attributes::Sequence.new(name, &block))
209+
def define_sequence(name, &)
210+
_attributes << attributes::Callable.new(name, self, attributes::Sequence.new(name, &))
209211
end
210212

211213
# @api private
@@ -219,9 +221,7 @@ def define_attr(name, *args, &block)
219221
end
220222

221223
# @api private
222-
def attributes
223-
::ROM::Factory::Attributes
224-
end
224+
def attributes = ::ROM::Factory::Attributes
225225
end
226226
end
227227
end

spec/integration/rom/factory_spec.rb

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,35 +1029,61 @@ class Admin < ROM::Struct
10291029

10301030
context "using associations" do
10311031
context "with traits" do
1032-
before do
1033-
factories.define(:user) do |f|
1034-
f.first_name "Jane"
1035-
f.last_name "Doe"
1036-
f.email "jane@doe.org"
1037-
f.timestamps
1038-
f.association(:tasks, :important, count: 2)
1039-
end
1032+
context "sequential" do
1033+
before do
1034+
factories.define(:user) do |f|
1035+
f.first_name "Jane"
1036+
f.last_name "Doe"
1037+
f.email "jane@doe.org"
1038+
f.timestamps
1039+
f.association(:tasks, :important, count: 2)
1040+
end
10401041

1041-
factories.define(:task) do |f|
1042-
f.sequence(:title) { |n| "Task #{n}" }
1043-
f.trait :important do |t|
1044-
t.sequence(:title) { |n| "Important Task #{n}" }
1042+
factories.define(:task) do |f|
1043+
f.sequence(:title) { |n| "Task #{n}" }
1044+
f.trait :important do |t|
1045+
t.sequence(:title) { |n| "Important Task #{n}" }
1046+
end
10451047
end
10461048
end
1047-
end
10481049

1049-
it "creates associated records with the given trait" do
1050-
user = factories[:user]
1050+
it "creates associated records with the given trait" do
1051+
user = factories[:user]
10511052

1052-
expect(user.tasks.count).to be(2)
1053+
expect(user.tasks.count).to be(2)
10531054

1054-
t1, t2 = user.tasks
1055+
t1, t2 = user.tasks
10551056

1056-
expect(t1.user_id).to be(user.id)
1057-
expect(t1.title).to eql("Important Task 1")
1057+
expect(t1.user_id).to be(user.id)
1058+
expect(t1.title).to eql("Important Task 1")
1059+
1060+
expect(t2.user_id).to be(user.id)
1061+
expect(t2.title).to eql("Important Task 2")
1062+
end
1063+
end
1064+
1065+
context "keyword" do
1066+
before do
1067+
factories.define(:user) do |f|
1068+
f.first_name "Jane"
1069+
f.last_name "Doe"
1070+
f.email "jane@doe.org"
1071+
f.timestamps
1072+
f.association(:tasks, count: 2, traits: [:important])
1073+
end
10581074

1059-
expect(t2.user_id).to be(user.id)
1060-
expect(t2.title).to eql("Important Task 2")
1075+
factories.define(:task) do |f|
1076+
f.sequence(:title) { |n| "Task #{n}" }
1077+
f.trait :important do |t|
1078+
t.sequence(:title) { |n| "Important Task #{n}" }
1079+
end
1080+
end
1081+
end
1082+
1083+
it "creates associated records with the given trait" do
1084+
user = factories[:user]
1085+
expect(user.tasks.count).to be(2)
1086+
end
10611087
end
10621088
end
10631089

0 commit comments

Comments
 (0)