Skip to content

Commit 8634a4f

Browse files
committed
allow accessing dependant attributes which have a reserve keyword as name
1 parent ee2462f commit 8634a4f

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/rom/factory/tuple_evaluator.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ def evaluate(*traits, **attrs)
8080
# @api private
8181
def evaluate_values(attrs)
8282
attributes.values.tsort.each_with_object({}) do |attr, h|
83-
deps = attr.dependency_names.map { |k| h[k] }.compact
83+
deps = attr.dependency_names.map do |key|
84+
h[remove_underscore(key)]
85+
end.compact
86+
8487
result = attr.(attrs, *deps)
8588

8689
if result
@@ -89,6 +92,16 @@ def evaluate_values(attrs)
8992
end
9093
end
9194

95+
# Extract underscore from end of symbol
96+
#
97+
# @param key [Symbol]
98+
# @return [Symbol]
99+
#
100+
# @api private
101+
def remove_underscore(key)
102+
key.to_s.chomp('_').to_sym
103+
end
104+
92105
def evaluate_traits(*traits, **attrs)
93106
return {} if traits.empty?
94107

spec/integration/rom/factory_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@
150150

151151
expect(user.email).to eql("#{user.first_name}.#{user.last_name}@test-1.org")
152152
end
153+
154+
it 'allows to access reserve keywords by apending an underscore at the end' do
155+
factories.define(:announcement) do |f|
156+
f.when { ROM::SQL::Postgres::Values::Range.new(3, 9, :'[]') }
157+
f.begin { |when_| when_.lower }
158+
f.end { |when_| when_.upper }
159+
end
160+
161+
announcement = factories[:announcement]
162+
163+
expect(announcement.begin).to eq 3
164+
expect(announcement.end).to eq 9
165+
end
153166
end
154167

155168
context 'incomplete schema' do

spec/shared/relations.rb

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
include_context 'database'
33

44
before do
5+
conn.extension(:pg_range)
56
conn.create_table(:users) do
67
primary_key :id
78
column :last_name, String, null: false
@@ -18,6 +19,17 @@
1819
column :title, String, null: false
1920
end
2021

22+
conn.create_table(:announcements) do
23+
primary_key :id
24+
numrange :when, null: false
25+
column :begin, Integer, null: false
26+
column :end, Integer, null: false
27+
end
28+
29+
conf.relation(:announcements) do
30+
schema(infer: true)
31+
end
32+
2133
conf.relation(:tasks) do
2234
schema(infer: true) do
2335
associations do
@@ -38,5 +50,6 @@
3850
after do
3951
conn.drop_table(:tasks)
4052
conn.drop_table(:users)
53+
conn.drop_table(:announcements)
4154
end
4255
end

0 commit comments

Comments
 (0)