aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-11-29 15:31:28 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2010-11-29 15:31:28 -0800
commitf68b7c4e7e25415c41308a5c37f1096a524b6d09 (patch)
treefb713329b508de307c9708f440b780bf3ae48a12
parentae4c814f537416345332b07bcaeaa199b75aa706 (diff)
downloadrails-f68b7c4e7e25415c41308a5c37f1096a524b6d09.tar.gz
rails-f68b7c4e7e25415c41308a5c37f1096a524b6d09.tar.bz2
rails-f68b7c4e7e25415c41308a5c37f1096a524b6d09.zip
base class works with visitor
-rw-r--r--History.txt5
-rw-r--r--lib/arel/nodes/node.rb2
-rw-r--r--lib/arel/visitors/depth_first.rb1
-rw-r--r--test/visitors/test_depth_first.rb6
4 files changed, 14 insertions, 0 deletions
diff --git a/History.txt b/History.txt
index 5e7ce2ee68..e981c26126 100644
--- a/History.txt
+++ b/History.txt
@@ -1,5 +1,10 @@
== 2.0.5 (unreleased)
+* Enhancements
+
+ * Arel::Visitors::DepthFirst can walk your AST depth first
+ * Arel::Nodes::Node is enumerable, depth first
+
* Bug fixes
* #lock will lock SELECT statements "FOR UPDATE" on mysql
diff --git a/lib/arel/nodes/node.rb b/lib/arel/nodes/node.rb
index 90c63f0be9..634e580a8f 100644
--- a/lib/arel/nodes/node.rb
+++ b/lib/arel/nodes/node.rb
@@ -37,6 +37,8 @@ module Arel
# Iterate through AST, nodes will be yielded depth-first
def each &block
+ return enum_for(:each) unless block_given?
+
Visitors::DepthFirst.new(block).accept self
end
end
diff --git a/lib/arel/visitors/depth_first.rb b/lib/arel/visitors/depth_first.rb
index 46171dc5bd..00f18727f0 100644
--- a/lib/arel/visitors/depth_first.rb
+++ b/lib/arel/visitors/depth_first.rb
@@ -93,6 +93,7 @@ module Arel
alias :visit_ActiveSupport_Multibyte_Chars :terminal
alias :visit_ActiveSupport_StringInquirer :terminal
alias :visit_Arel_Nodes_Lock :terminal
+ alias :visit_Arel_Nodes_Node :terminal
alias :visit_Arel_Nodes_SqlLiteral :terminal
alias :visit_Arel_SqlLiteral :terminal
alias :visit_BigDecimal :terminal
diff --git a/test/visitors/test_depth_first.rb b/test/visitors/test_depth_first.rb
index 2f7c832bde..4015d72254 100644
--- a/test/visitors/test_depth_first.rb
+++ b/test/visitors/test_depth_first.rb
@@ -192,6 +192,12 @@ module Arel
@visitor.accept stmt
assert_equal [:a, :b, stmt.columns, :c, stmt], @collector.calls
end
+
+ def test_node
+ node = Nodes::Node.new
+ @visitor.accept node
+ assert_equal [node], @collector.calls
+ end
end
end
end