Class: Metamorpher::Visitable::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/metamorpher/visitable/visitor.rb

Instance Method Summary collapse

Instance Method Details

#visit(thing) ⇒ Object

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.



11
12
13
14
15
16
17
18
# File 'lib/metamorpher/visitable/visitor.rb', line 11

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