forked from brynary/arel
-
Notifications
You must be signed in to change notification settings - Fork 385
Expand file tree
/
Copy pathvisitor.rb
More file actions
34 lines (30 loc) · 866 Bytes
/
Copy pathvisitor.rb
File metadata and controls
34 lines (30 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Arel
module Visitors
class Visitor
def accept object
visit object
end
private
DISPATCH = Hash.new do |hash, visitor_class|
hash[visitor_class] =
Hash.new do |method_hash, node_class|
method_hash[node_class] = "visit_#{(node_class.name || '').gsub('::', '_')}"
end
end
def dispatch
DISPATCH[self.class]
end
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
end
end
end
end