Sha256: 176bb2f6b1b257f9ef7173e1d6caa871c8b50d1130a263a4d2e07509937b3a0a
Contents?: true
Size: 1.09 KB
Versions: 1
Compression:
Stored size: 1.09 KB
Contents
module Visitor def visit(obj) method = _visitor_method(obj) self.send(method, obj) if method end def visit_me_and_my_children(me, children) visit(me) children.each {|c| c.accept_visitor(self)} end def visit_me_then_children(obj, *children) visit(obj) children.each {|cs| cs.each {|c| c.accept_visitor(self)}} end # Find the visit_X method to use based on the inheritance tree. First try # the class name then traverse the ancestors list. def _visitor_method(obj) obj.class.ancestors.map {|c| _visitor_method_for_class(c)}.compact.first end def _visitor_method_for_class(klass) _visitor_methods[klass] ||= _methodname_from_class(klass) end def _visitor_methods @_visitor_methods ||= Hash.new end def _methodname_from_class(klass) name = "visit_" + klass.inspect.split("::").last self.class.instance_methods.include?(name) ? name : nil end def visit_each(ary) ary.each {|e| e.accept_visitor(self)} end end module Visitable def accept_visitor(visitor) visitor.visit(self) end end class Object include Visitable end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rockit-0.7.1 | lib/util/visitor.rb |