Skip to content

Commit

Permalink
Added NotMatch and NotIn predicates, made Not derive from Equality (r…
Browse files Browse the repository at this point in the history
…everted later)
  • Loading branch information
Ernie Miller committed May 7, 2010
1 parent 7aff5ac commit def75c7
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 8 deletions.
8 changes: 8 additions & 0 deletions lib/arel/algebra/attributes/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,21 @@ def matches(regexp)
Predicates::Match.new(self, regexp)
end

def notmatches(regexp)
Predicates::NotMatch.new(self, regexp)
end

def in(array)
if array.is_a?(Range) && array.exclude_end?
[Predicates::GreaterThanOrEqualTo.new(self, array.begin), Predicates::LessThan.new(self, array.end)]
else
Predicates::In.new(self, array)
end
end

def notin(array)
Predicates::NotIn.new(self, array)
end
end
include Predications

Expand Down
16 changes: 9 additions & 7 deletions lib/arel/algebra/predicates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ def ==(other)
end
end

class Not < Binary; end
class GreaterThanOrEqualTo < Binary; end
class GreaterThan < Binary; end
class LessThanOrEqualTo < Binary; end
class LessThan < Binary; end
class Match < Binary; end
class In < Binary; end
class Not < Equality; end
class GreaterThanOrEqualTo < Binary; end
class GreaterThan < Binary; end
class LessThanOrEqualTo < Binary; end
class LessThan < Binary; end
class Match < Binary; end
class NotMatch < Binary; end
class In < Binary; end
class NotIn < Binary; end
end
end
12 changes: 11 additions & 1 deletion lib/arel/engines/memory/predicates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Equality < Binary
def operator; :== end
end

class Not < Binary
class Not < Equality
def eval(row)
operand1.eval(row) != operand2.eval(row)
end
Expand All @@ -35,9 +35,19 @@ def operator; :< end
class Match < Binary
def operator; :=~ end
end

class NotMatch < Binary
def operator; :!~ end
end

class In < Binary
def operator; :include? end
end

class NotIn < Binary
def eval(row)
!(operand1.eval(row).include?(operand2.eval(row)))
end
end
end
end
4 changes: 4 additions & 0 deletions lib/arel/engines/sql/core_extensions/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def to_sql(formatter = nil)
def inclusion_predicate_sql
"IN"
end

def exclusion_predicate_sql
"NOT IN"
end

Array.send(:include, self)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/arel/engines/sql/core_extensions/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def to_sql(formatter = nil)
def inclusion_predicate_sql
"BETWEEN"
end

def exclusion_predicate_sql
"NOT BETWEEN"
end

Range.send(:include, self)
end
Expand Down
8 changes: 8 additions & 0 deletions lib/arel/engines/sql/predicates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@ def predicate_sql; '<' end
class Match < Binary
def predicate_sql; 'LIKE' end
end

class NotMatch < Binary
def predicate_sql; 'NOT LIKE' end
end

class In < Binary
def predicate_sql; operand2.inclusion_predicate_sql end
end

class NotIn < Binary
def predicate_sql; operand2.exclusion_predicate_sql end
end
end
end
4 changes: 4 additions & 0 deletions lib/arel/engines/sql/relations/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def christener
def inclusion_predicate_sql
"IN"
end

def exclusion_predicate_sql
"NOT IN"
end

def primary_key
connection_id = engine.connection.object_id
Expand Down
4 changes: 4 additions & 0 deletions spec/shared/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@
end

it "finds rows with a matches predicate"

it "finds rows with a not 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

it "finds rows with a not in predicate"
end

describe "#order" do
Expand Down

0 comments on commit def75c7

Please sign in to comment.