Sha256: da33f83b6fabb77012875ebae73d8d220424b023140d0edfefb7f60d82a6f295

Contents?: true

Size: 979 Bytes

Versions: 4

Compression:

Stored size: 979 Bytes

Contents

# frozen_string_literal: true
module Arel
  module Visitors
    class Visitor
      def initialize
        @dispatch = get_dispatch_cache
      end

      def accept object
        visit object
      end

      private

      def self.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 dispatch
        @dispatch
      end

      def visit object
        dispatch_method = dispatch[object.class]
        send dispatch_method, object
      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

4 entries across 4 versions & 3 rubygems

Version Path
spiral_form-0.1.1 vendor/bundle/gems/arel-9.0.0/lib/arel/visitors/visitor.rb
spiral_form-0.1.0 vendor/bundle/gems/arel-9.0.0/lib/arel/visitors/visitor.rb
nullifyable-0.1.0 vendor/bundle/gems/arel-9.0.0/lib/arel/visitors/visitor.rb
arel-9.0.0 lib/arel/visitors/visitor.rb