Skip to content

Commit

Permalink
allow accessing dependant attributes which have a reserve keyword as …
Browse files Browse the repository at this point in the history
…name
  • Loading branch information
GustavoCaso committed Mar 24, 2018
1 parent ee2462f commit 317cf70
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/rom/factory/tuple_evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ def evaluate(*traits, **attrs)
# @api private
def evaluate_values(attrs)
attributes.values.tsort.each_with_object({}) do |attr, h|
deps = attr.dependency_names.map { |k| h[k] }.compact
deps = attr.dependency_names.map do |key|
h[dependecy_name(key)]
end.compact

result = attr.(attrs, *deps)

if result
Expand All @@ -89,6 +92,15 @@ def evaluate_values(attrs)
end
end

# @param key [Symbol]
# @return [Symbol]
#
# @api private
def dependecy_name(key)
key.to_s.chomp('_').to_sym
end

# @api private
def evaluate_traits(*traits, **attrs)
return {} if traits.empty?

Expand Down
13 changes: 13 additions & 0 deletions spec/integration/rom/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@

expect(user.email).to eql("#{user.first_name}.#{user.last_name}@test-1.org")
end

it 'allows to access reserve keywords by apending an underscore at the end' do
factories.define(:announcement) do |f|
f.when { ROM::SQL::Postgres::Values::Range.new(3, 9, :'[]') }
f.begin { |when_| when_.lower }
f.end { |when_| when_.upper }
end

announcement = factories[:announcement]

expect(announcement.begin).to eq 3
expect(announcement.end).to eq 9
end
end

context 'incomplete schema' do
Expand Down
13 changes: 13 additions & 0 deletions spec/shared/relations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
include_context 'database'

before do
conn.extension(:pg_range)
conn.create_table(:users) do
primary_key :id
column :last_name, String, null: false
Expand All @@ -18,6 +19,17 @@
column :title, String, null: false
end

conn.create_table(:announcements) do
primary_key :id
numrange :when, null: false
column :begin, Integer, null: false
column :end, Integer, null: false
end

conf.relation(:announcements) do
schema(infer: true)
end

conf.relation(:tasks) do
schema(infer: true) do
associations do
Expand All @@ -38,5 +50,6 @@
after do
conn.drop_table(:tasks)
conn.drop_table(:users)
conn.drop_table(:announcements)
end
end

0 comments on commit 317cf70

Please sign in to comment.