diff options
-rw-r--r-- | History.txt | 5 | ||||
-rw-r--r-- | lib/arel/nodes/node.rb | 2 | ||||
-rw-r--r-- | lib/arel/visitors/depth_first.rb | 1 | ||||
-rw-r--r-- | test/visitors/test_depth_first.rb | 6 |
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 |