Module: RKelly::Visitable

Included in:
Nodes::Node
Defined in:
lib/rkelly/visitable.rb

Instance Method Summary collapse

Instance Method Details

#accept(visitor, &block) ⇒ Object

Based off the visitor pattern from RubyGarden



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/rkelly/visitable.rb', line 4

def accept(visitor, &block)
  klass = self.class.ancestors.find { |ancestor|
    visitor.respond_to?("visit_#{ancestor.name.split(/::/)[-1]}")
  }

  if klass
    visitor.send(:"visit_#{klass.name.split(/::/)[-1]}", self, &block)
  else
    raise "No visitor for '#{self.class}'"
  end
end