aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarl Lerche <carllerche@mac.com>2010-03-12 15:25:21 -0800
committerCarl Lerche <carllerche@mac.com>2010-03-12 15:25:21 -0800
commit0e6888232a19c8c59416490d3da6079e590fab77 (patch)
tree43f5157375fdcfb449f245f43dcddabd29208243
parente13420c86afb5c31e90cff800f121bd49255b939 (diff)
downloadrails-0e6888232a19c8c59416490d3da6079e590fab77.tar.gz
rails-0e6888232a19c8c59416490d3da6079e590fab77.tar.bz2
rails-0e6888232a19c8c59416490d3da6079e590fab77.zip
Add support for a NOT predicate
-rw-r--r--lib/arel/algebra/attributes/attribute.rb4
-rw-r--r--lib/arel/algebra/predicates.rb1
-rw-r--r--lib/arel/engines/memory/predicates.rb6
-rw-r--r--lib/arel/engines/sql/predicates.rb4
-rw-r--r--spec/algebra/integration/basic_spec.rb33
5 files changed, 48 insertions, 0 deletions
diff --git a/lib/arel/algebra/attributes/attribute.rb b/lib/arel/algebra/attributes/attribute.rb
index 03cf44a552..b372be5e1d 100644
--- a/lib/arel/algebra/attributes/attribute.rb
+++ b/lib/arel/algebra/attributes/attribute.rb
@@ -86,6 +86,10 @@ module Arel
Predicates::Equality.new(self, other)
end
+ def not(other)
+ Predicates::Not.new(self, other)
+ end
+
def lt(other)
Predicates::LessThan.new(self, other)
end
diff --git a/lib/arel/algebra/predicates.rb b/lib/arel/algebra/predicates.rb
index 8a0b41fa18..700cd6afaa 100644
--- a/lib/arel/algebra/predicates.rb
+++ b/lib/arel/algebra/predicates.rb
@@ -33,6 +33,7 @@ module Arel
end
end
+ class Not < Binary; end
class GreaterThanOrEqualTo < Binary; end
class GreaterThan < Binary; end
class LessThanOrEqualTo < Binary; end
diff --git a/lib/arel/engines/memory/predicates.rb b/lib/arel/engines/memory/predicates.rb
index dd2559c70f..f87bf68357 100644
--- a/lib/arel/engines/memory/predicates.rb
+++ b/lib/arel/engines/memory/predicates.rb
@@ -10,6 +10,12 @@ module Arel
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
diff --git a/lib/arel/engines/sql/predicates.rb b/lib/arel/engines/sql/predicates.rb
index e563a9ba3e..3756231a46 100644
--- a/lib/arel/engines/sql/predicates.rb
+++ b/lib/arel/engines/sql/predicates.rb
@@ -26,6 +26,10 @@ module Arel
end
end
+ class Not < Binary
+ def predicate_sql; '!=' end
+ end
+
class GreaterThanOrEqualTo < Binary
def predicate_sql; '>=' end
end
diff --git a/spec/algebra/integration/basic_spec.rb b/spec/algebra/integration/basic_spec.rb
index 7aa4f7305c..e48f94625d 100644
--- a/spec/algebra/integration/basic_spec.rb
+++ b/spec/algebra/integration/basic_spec.rb
@@ -49,6 +49,39 @@ share_examples_for 'A Relation' do
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