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

Commit

Permalink
OrOperation should be valid if at least one branch is valid
Browse files Browse the repository at this point in the history
OrOperations are a bit different from the other operations, since
they are valid if at least one of the branches it contains is valid.

A future refactor should also ensure that only the valid operations
go to the datastore, this can also eliminate OR branches of only
one valid branch is left.
  • Loading branch information
dbussink committed Oct 27, 2009
1 parent 28a1222 commit b543106
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/dm-core/query/conditions/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ class OrOperation < AbstractOperation
def matches?(record)
@operands.any? { |operand| operand.matches?(record) }
end

def valid?
@operands.any? do |operand|
if operand.respond_to?(:valid?)
operand.valid?
else
true
end
end
end

end # class OrOperation

class NotOperation < AbstractOperation
Expand Down
13 changes: 13 additions & 0 deletions spec/semipublic/query/conditions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class ::Heffalump
before do
@comp1 = Comparison.new(:eql, Heffalump.num_spots, 1)
@comp2 = Comparison.new(:eql, Heffalump.color, 'green')
@comp3 = Comparison.new(:in, Heffalump.mass, [])
@comp4 = Comparison.new(:in, Heffalump.parent, [])
end

it 'should initialize an AbstractOperation object' do
Expand Down Expand Up @@ -141,6 +143,8 @@ class ::Heffalump
describe 'OrOperation' do
before do
@op = Operation.new(:or, @comp1, @comp2)
@op1 = Operation.new(:or, @comp1, @comp3)
@op2 = Operation.new(:or, @comp3, @comp4)
end

it 'should match if any of the comparisons match' do
Expand All @@ -156,6 +160,15 @@ class ::Heffalump

@op.should_not match(@heff3)
end

it 'should be valid if at least one branch is valid' do
@op1.should be_valid
end

it 'should not be valid if there are no valid branches' do
@op2.should_not be_valid
end

end
end

Expand Down
27 changes: 27 additions & 0 deletions spec/semipublic/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,33 @@ class ::Contact < User; end
end
end

describe 'with an Array with no entries' do
before :all do
@options[:conditions] = { :name => [] }
@return = DataMapper::Query.new(@repository, @model, @options.freeze)
end

it { @return.should be_kind_of(DataMapper::Query) }

it 'should set the conditions' do
pending do
@return.conditions.should ==
DataMapper::Query::Conditions::Operation.new(
:and,
DataMapper::Query::Conditions::Comparison.new(
:eql,
@model.properties[:name],
'Dan Kubb'
)
)
end
end

it 'should not be valid' do
@return.should_not be_valid
end
end

describe 'with an Array with duplicate entries' do
before :all do
@options[:conditions] = { :name => [ 'John Doe', 'Dan Kubb', 'John Doe' ] }
Expand Down

0 comments on commit b543106

Please sign in to comment.