Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Change comparison operator based on inclusive vs exclusive ranges #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions lib/dm-do-adapter/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,11 @@ def comparison_statement(comparison, qualify)
# update conditions_statement to use it

# break exclusive Range queries up into two comparisons ANDed together
if value.kind_of?(Range) && value.exclude_end?
if value.kind_of?(Range)
comparator = value.exclude_end? ? :lt : :lte
operation = Query::Conditions::Operation.new(:and,
Query::Conditions::Comparison.new(:gte, subject, value.first),
Query::Conditions::Comparison.new(:lt, subject, value.last)
Query::Conditions::Comparison.new(comparator, subject, value.last)
)

statement, bind_values = conditions_statement(operation, qualify)
Expand Down
22 changes: 22 additions & 0 deletions lib/dm-do-adapter/spec/shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,28 @@ class ::Author
end
end

context 'with an range with inclusive end' do
before :all do
5.times do |index|
@article_model.create(:name => "Test #{index}", :parent => @article_model.last).should be_saved
end
end

it 'should not call #partition on the range' do
range = 1..5

query = DataMapper::Query.new(repository, @article_model, :parent_name => range)

# We have to get the dumped value, because the original value has been
# dupped multiple times
val = query.conditions.operands.first.send(:dumped_value)
val.stub(:partition)
val.should_not_receive(:partition)

@adapter.read(query)
end
end

context 'with an inclusion comparison of nil values' do
before :all do
5.times do |index|
Expand Down