Module: RKelly::Visitable

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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



17
18
19
# File 'lib/rkelly/visitable.rb', line 17

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#accept(visitor, &block) ⇒ Object

Based off the visitor pattern from RubyGarden



23
24
25
26
27
28
29
# 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