Skip to content

Commit

Permalink
push reduction visitors to a reduction base class
Browse files Browse the repository at this point in the history
this lets our old depth first and dot visitors to work normally
  • Loading branch information
tenderlove committed Apr 9, 2014
1 parent 6ae60fd commit a6a7c75
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
17 changes: 3 additions & 14 deletions lib/arel/visitors/depth_first.rb
Expand Up @@ -5,22 +5,11 @@ def initialize block = nil
@block = block || Proc.new
end

def accept object
visit object
end

private

def visit object
send dispatch[object.class], object
rescue NoMethodError => e
raise e if respond_to?(dispatch[object.class], true)
superklass = object.class.ancestors.find { |klass|
respond_to?(dispatch[klass], true)
}
raise(TypeError, "Cannot visit #{object.class}") unless superklass
dispatch[object.class] = dispatch[superklass]
retry
def visit o
super
@block.call o
end

def unary o
Expand Down
3 changes: 2 additions & 1 deletion lib/arel/visitors/dot.rb
Expand Up @@ -23,11 +23,12 @@ def initialize
end

def accept object
super
visit object
to_dot
end

private

def visit_Arel_Nodes_Ordering o
visit_edge o, "expr"
end
Expand Down
25 changes: 25 additions & 0 deletions lib/arel/visitors/reduce.rb
@@ -0,0 +1,25 @@
require 'arel/visitors/visitor'

module Arel
module Visitors
class Reduce < Arel::Visitors::Visitor
def accept object, collector
visit object, collector
end

private

def visit object, collector
send dispatch[object.class], object, collector
rescue NoMethodError => e
raise e if respond_to?(dispatch[object.class], true)
superklass = object.class.ancestors.find { |klass|
respond_to?(dispatch[klass], true)
}
raise(TypeError, "Cannot visit #{object.class}") unless superklass
dispatch[object.class] = dispatch[superklass]
retry
end
end
end
end
3 changes: 2 additions & 1 deletion lib/arel/visitors/to_sql.rb
@@ -1,9 +1,10 @@
require 'bigdecimal'
require 'date'
require 'arel/visitors/reduce'

module Arel
module Visitors
class ToSql < Arel::Visitors::Visitor
class ToSql < Arel::Visitors::Reduce
##
# This is some roflscale crazy stuff. I'm roflscaling this because
# building SQL queries is a hotspot. I will explain the roflscale so that
Expand Down
8 changes: 4 additions & 4 deletions lib/arel/visitors/visitor.rb
@@ -1,8 +1,8 @@
module Arel
module Visitors
class Visitor
def accept object, collector
visit object, collector
def accept object
visit object
end

private
Expand All @@ -18,8 +18,8 @@ def dispatch
DISPATCH[self.class]
end

def visit object, collector
send dispatch[object.class], object, collector
def visit object
send dispatch[object.class], object
rescue NoMethodError => e
raise e if respond_to?(dispatch[object.class], true)
superklass = object.class.ancestors.find { |klass|
Expand Down
2 changes: 1 addition & 1 deletion test/visitors/test_to_sql.rb
Expand Up @@ -23,7 +23,7 @@ def compile node

it 'can define a dispatch method' do
visited = false
viz = Class.new(Arel::Visitors::Visitor) {
viz = Class.new(Arel::Visitors::Reduce) {
define_method(:hello) do |node, c|
visited = true
end
Expand Down

0 comments on commit a6a7c75

Please sign in to comment.