module Filigree::Visitable
This module provides a default implementation of three common traversal patterns: pre-order, post-order, and in-order (level-order). The including class must implement the `children` function.
Public Instance Methods
visit(visitor, method = :preorder)
click to toggle source
Visit this object with the provided visitor in pre-, post-, or in-order traversal.
@param [Visitor] visitor Visitor
to call @param [:preorder, :inorder, :postorder, :downup] method How to visit
@return [Boolean] If a pattern matched or not
# File lib/filigree/visitor.rb, line 237 def visit(visitor, method = :preorder) case method when :preorder res = (visitor.visit(self) != MatchError) children.flatten.compact.inject(false) { |mod, child| child.visit(visitor, :preorder) || mod } || res when :inorder lmod = false nodes = [self] # Not all Ruby implementations support modifying arrays while # you are iterating over them. while node = nodes.shift nodes += node.children.flatten.compact lmod = visitor.visit(node) || lmod end lmod when :postorder res = children.flatten.compact.inject(false) { |modified, child| child.visit(visitor, :postorder) || modified } (visitor.visit(self) != MatchError) || res when :downup res = (visitor.visit(self) != MatchError) res = children.flatten.compact.inject(false) { |modified, child| child.visit(visitor, :downup) || modified } || res (visitor.visit(self) != MatchError) || res end end