Sha256: 4896a99f9d16cfba855bf4b317745575c44dffe03a6ac2922ea9dc47c9b55263

Contents?: true

Size: 755 Bytes

Versions: 9

Compression:

Stored size: 755 Bytes

Contents

# Based on http://blog.rubybestpractices.com/posts/aaronp/001_double_dispatch_dance.html

module Metamorpher
  module Visitable
    class Visitor
      ###
      # This method will examine the class and ancestors of +thing+. For each
      # class in the "ancestors" list, it will check to see if the visitor knows
      # how to handle that particular class. If it can't find a handler for the
      # +thing+ it will raise an exception.
      def visit(thing)
        thing.class.ancestors.each do |ancestor|
          method_name = :"visit_#{ancestor.name.split("::").last.downcase}"
          return send(method_name, thing) if respond_to?(method_name)
        end

        fail ArgumentError, "Can't visit #{thing.class}"
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
metamorpher-0.2.6 lib/metamorpher/visitable/visitor.rb
metamorpher-0.2.5 lib/metamorpher/visitable/visitor.rb
metamorpher-0.2.4 lib/metamorpher/visitable/visitor.rb
metamorpher-0.2.3 lib/metamorpher/visitable/visitor.rb
metamorpher-0.2.2 lib/metamorpher/visitable/visitor.rb
metamorpher-0.2.1 lib/metamorpher/visitable/visitor.rb
metamorpher-0.2.0 lib/metamorpher/visitable/visitor.rb
metamorpher-0.1.1 lib/metamorpher/visitable/visitor.rb
metamorpher-0.1.0 lib/metamorpher/visitable/visitor.rb