Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow accessing dependant attributes which have a reserve keyword as name #22

Closed
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
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