module RKelly::Visitable

Public Class Methods

included(klass) click to toggle source
# File lib/rkelly/visitable.rb, line 17
def self.included(klass)
  klass.extend(ClassMethods)
end

Public Instance Methods

accept(visitor, &block) click to toggle source

Based off the visitor pattern from RubyGarden

# File lib/rkelly/visitable.rb, line 23
def accept(visitor, &block)
  klass, meth = self.class.visitor_method_names_by_ancestors.find do |meth|
    return visitor.send(meth, self, &block) if visitor.respond_to?(meth)
  end

  raise "No visitor for '#{self.class}'"
end