Sha256: 91c9f1c3fd30be8aa052a5eab35fc856e91281d1737a2e75f0885fba551172b2

Contents?: true

Size: 914 Bytes

Versions: 30

Compression:

Stored size: 914 Bytes

Contents

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
        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

Version data entries

30 entries across 29 versions & 9 rubygems

Version Path
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/arel-6.0.4/lib/arel/visitors/visitor.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/arel-7.1.4/lib/arel/visitors/visitor.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/arel-7.1.4/lib/arel/visitors/visitor.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/arel-7.1.4/lib/arel/visitors/visitor.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/arel-7.1.4/lib/arel/visitors/visitor.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/arel-7.1.4/lib/arel/visitors/visitor.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/arel-7.1.4/lib/arel/visitors/visitor.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/arel-7.1.4/lib/arel/visitors/visitor.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/arel-7.1.4/lib/arel/visitors/visitor.rb
abaci-0.3.0 vendor/bundle/gems/arel-7.1.0/lib/arel/visitors/visitor.rb
arel-6.0.4 lib/arel/visitors/visitor.rb
arel-7.1.4 lib/arel/visitors/visitor.rb
arel-7.1.3 lib/arel/visitors/visitor.rb
arel-7.1.2 lib/arel/visitors/visitor.rb
arel-7.1.1 lib/arel/visitors/visitor.rb
arel-7.1.0 lib/arel/visitors/visitor.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/arel-6.0.3/lib/arel/visitors/visitor.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/arel-6.0.3/lib/arel/visitors/visitor.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/arel-6.0.3/lib/arel/visitors/visitor.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/arel-6.0.3/lib/arel/visitors/visitor.rb