Sha256: 97284b730b18ab38c55557631790c73ba1636f6d3c14ebb87b9a5ceb11ddfd8c
Contents?: true
Size: 1.18 KB
Versions: 42
Compression:
Stored size: 1.18 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.compare_by_identity 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
42 entries across 42 versions & 5 rubygems