Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for a NOT predicate

  • Loading branch information...
commit 0e6888232a19c8c59416490d3da6079e590fab77 1 parent e13420c
Carl Lerche authored
View
4 lib/arel/algebra/attributes/attribute.rb
@@ -86,6 +86,10 @@ def eq(other)
Predicates::Equality.new(self, other)
end
+ def not(other)
+ Predicates::Not.new(self, other)
+ end
+
def lt(other)
Predicates::LessThan.new(self, other)
end
View
1  lib/arel/algebra/predicates.rb
@@ -33,6 +33,7 @@ def ==(other)
end
end
+ class Not < Binary; end
class GreaterThanOrEqualTo < Binary; end
class GreaterThan < Binary; end
class LessThanOrEqualTo < Binary; end
View
6 lib/arel/engines/memory/predicates.rb
@@ -10,6 +10,12 @@ class Equality < Binary
def operator; :== end
end
+ class Not < Binary
+ def eval(row)
+ operand1.eval(row) != operand2.eval(row)
+ end
+ end
+
class GreaterThanOrEqualTo < Binary
def operator; :>= end
end
View
4 lib/arel/engines/sql/predicates.rb
@@ -26,6 +26,10 @@ def predicate_sql
end
end
+ class Not < Binary
+ def predicate_sql; '!=' end
+ end
+
class GreaterThanOrEqualTo < Binary
def predicate_sql; '>=' end
end
View
33 spec/algebra/integration/basic_spec.rb
@@ -49,6 +49,39 @@ def have_rows(expected)
expected = @expected.select { |r| r[@relation[:age]] == @pivot[@relation[:age]] }
@relation.where(@relation[:age].eq(@pivot[@relation[:age]])).should have_rows(expected)
end
+
+ it "finds rows with a not predicate" do
+ expected = @expected.select { |r| r[@relation[:age]] != @pivot[@relation[:age]] }
+ @relation.where(@relation[:age].not(@pivot[@relation[:age]])).should have_rows(expected)
+ end
+
+ it "finds rows with a less than predicate" do
+ expected = @expected.select { |r| r[@relation[:age]] < @pivot[@relation[:age]] }
+ @relation.where(@relation[:age].lt(@pivot[@relation[:age]])).should have_rows(expected)
+ end
+
+ it "finds rows with a less than or equal to predicate" do
+ expected = @expected.select { |r| r[@relation[:age]] <= @pivot[@relation[:age]] }
+ @relation.where(@relation[:age].lteq(@pivot[@relation[:age]])).should have_rows(expected)
+ end
+
+ it "finds rows with a greater than predicate" do
+ expected = @expected.select { |r| r[@relation[:age]] > @pivot[@relation[:age]] }
+ @relation.where(@relation[:age].gt(@pivot[@relation[:age]])).should have_rows(expected)
+ end
+
+ it "finds rows with a greater than or equal to predicate" do
+ expected = @expected.select { |r| r[@relation[:age]] >= @pivot[@relation[:age]] }
+ @relation.where(@relation[:age].gteq(@pivot[@relation[:age]])).should have_rows(expected)
+ end
+
+ it "finds rows with a matches predicate"
+
+ it "finds rows with an in predicate" do
+ pending
+ set = @expected[1..(@expected.length/2+1)]
+ @relation.all(:id.in => set.map { |r| r.id }).should have_resources(set)
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.