Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
adding a "not" factory method for creating Not nodes
  • Loading branch information
tenderlove committed Nov 24, 2010
1 parent 3e928ee commit 76932b9
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions History.txt
Expand Up @@ -3,6 +3,7 @@
* Bug fixes

* #lock will lock SELECT statements "FOR UPDATE" on mysql
* Nodes::Node#not factory method added for creating Nodes::Not nodes

== 2.0.4

Expand Down
2 changes: 2 additions & 0 deletions Manifest.txt
Expand Up @@ -43,6 +43,7 @@ lib/arel/nodes/matches.rb
lib/arel/nodes/max.rb
lib/arel/nodes/min.rb
lib/arel/nodes/node.rb
lib/arel/nodes/not.rb
lib/arel/nodes/not_equal.rb
lib/arel/nodes/not_in.rb
lib/arel/nodes/offset.rb
Expand Down Expand Up @@ -84,6 +85,7 @@ test/nodes/test_count.rb
test/nodes/test_delete_statement.rb
test/nodes/test_equality.rb
test/nodes/test_insert_statement.rb
test/nodes/test_not.rb
test/nodes/test_or.rb
test/nodes/test_select_core.rb
test/nodes/test_select_statement.rb
Expand Down
1 change: 1 addition & 0 deletions lib/arel/nodes.rb
Expand Up @@ -6,6 +6,7 @@
require 'arel/nodes/assignment'
require 'arel/nodes/or'
require 'arel/nodes/and'
require 'arel/nodes/not'
require 'arel/nodes/greater_than'
require 'arel/nodes/greater_than_or_equal'
require 'arel/nodes/less_than'
Expand Down
7 changes: 7 additions & 0 deletions lib/arel/nodes/node.rb
Expand Up @@ -3,6 +3,13 @@ module Nodes
###
# Abstract base class for all AST nodes
class Node
###
# Factory method to create a Nodes::Not node that has the recipient of
# the caller as a child.
def not
Nodes::Not.new self
end

###
# Factory method to create a Nodes::Grouping node that has an Nodes::Or
# node as a child.
Expand Down
11 changes: 11 additions & 0 deletions lib/arel/nodes/not.rb
@@ -0,0 +1,11 @@
module Arel
module Nodes
class Not < Arel::Nodes::Node
attr_reader :expr

def initialize expr
@expr = expr
end
end
end
end
4 changes: 4 additions & 0 deletions lib/arel/visitors/to_sql.rb
Expand Up @@ -191,6 +191,10 @@ def visit_Arel_Nodes_On o
"ON #{visit o.expr}"
end

def visit_Arel_Nodes_Not o
"NOT #{visit o.expr}"
end

def visit_Arel_Table o
if o.table_alias
"#{quote_table_name o.name} #{quote_table_name o.table_alias}"
Expand Down
20 changes: 20 additions & 0 deletions test/nodes/test_not.rb
@@ -0,0 +1,20 @@
require 'helper'

module Arel
module Nodes
describe 'not' do
describe '#not' do
it 'makes a NOT node' do
attr = Table.new(:users)[:id]
left = attr.eq(10)
right = attr.eq(11)
node = left.or right
node.expr.left.must_equal left
node.expr.right.must_equal right

knot = node.or(right).not
end
end
end
end
end
5 changes: 5 additions & 0 deletions test/visitors/test_to_sql.rb
Expand Up @@ -33,6 +33,11 @@ module Visitors
@visitor.accept 2.14
end

it "should visit_Not" do
sql = @visitor.accept Nodes::Not.new(Arel.sql("foo"))
sql.must_be_like "NOT foo"
end

it "should visit_Bignum" do
@visitor.accept 8787878092
end
Expand Down

0 comments on commit 76932b9

Please sign in to comment.