Module: Johnson::Visitable

Included in:
Nodes::Node
Defined in:
lib/johnson/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/johnson/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}' at line #{line}"
  end
end