Sha256: b058cd1018d54352e0e5324409fb60a4d11448fb3f436321cc35ffa122e5d1bb

Contents?: true

Size: 1.16 KB

Versions: 100

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Arel # :nodoc: all
  module Visitors
    class Visitor
      def initialize
        @dispatch = get_dispatch_cache
      end

      def accept(object, collector = nil)
        visit object, collector
      end

      private
        attr_reader :dispatch

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

        def get_dispatch_cache
          self.class.dispatch_cache
        end

        def visit(object, collector = nil)
          dispatch_method = dispatch[object.class]
          if collector
            send dispatch_method, object, collector
          else
            send dispatch_method, object
          end
        rescue NoMethodError => e
          raise e if respond_to?(dispatch_method, 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

Version data entries

100 entries across 96 versions & 7 rubygems

Version Path
activerecord-7.0.8.6 lib/arel/visitors/visitor.rb
activerecord-6.1.7.10 lib/arel/visitors/visitor.rb
activerecord-6.1.7.9 lib/arel/visitors/visitor.rb
activerecord-7.0.8.5 lib/arel/visitors/visitor.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8.4/lib/arel/visitors/visitor.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/arel/visitors/visitor.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/arel/visitors/visitor.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/arel/visitors/visitor.rb
activerecord-7.0.8.4 lib/arel/visitors/visitor.rb
activerecord-6.1.7.8 lib/arel/visitors/visitor.rb
activerecord-7.0.8.1 lib/arel/visitors/visitor.rb
activerecord-6.1.7.7 lib/arel/visitors/visitor.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/arel/visitors/visitor.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/arel/visitors/visitor.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-6.1.6.1/lib/arel/visitors/visitor.rb
activerecord-7.0.8 lib/arel/visitors/visitor.rb
activerecord-7.0.7.2 lib/arel/visitors/visitor.rb
activerecord-6.1.7.6 lib/arel/visitors/visitor.rb
activerecord-7.0.7.1 lib/arel/visitors/visitor.rb
activerecord-6.1.7.5 lib/arel/visitors/visitor.rb