aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/visitors/visitor.rb
blob: b0d16ffe4ee0ce46820e6f18d7cd6c5a866a8a8d (plain) (blame)
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
module Arel
  module Visitors
    class Visitor
      def accept object
        visit object
      end

      private

      DISPATCH = Hash.new do |hash, klass|
        hash[klass] = "visit_#{klass.name.gsub('::', '_')}"
      end

      def visit object
        send DISPATCH[object.class], object
      rescue NoMethodError
        warn "visiting #{object.class} via superclass, this will be removed in arel 2.2.0" if $VERBOSE
        superklass = object.class.ancestors.find { |klass|
          respond_to?(DISPATCH[klass], true)
        }
        DISPATCH[object.class] = DISPATCH[superklass]
        retry
      end
    end
  end
end