lib/treevisitor/tree_node.rb in treevisitor-0.1.6 vs lib/treevisitor/tree_node.rb in treevisitor-0.2.0
- old
+ new
@@ -168,24 +168,28 @@
self
end
#
# Find a node down the hierarchy with content
- # @param [Object] content of searched node
- # @return [Object, nil] nil if no
+ # @param [Object,Regexp] content of searched node
+ # @return [Object, nil] nil if not found
#
def find(content = nil, &block)
if content and block_given?
raise "TreeNode::find - passed content AND block"
end
if content
- block = proc { |c| c == content }
+ if content.class == Regexp
+ block = proc { |l| l.content =~ content }
+ else
+ block = proc { |l| l.content == content }
+ end
end
- return self if block.call(self.content)
+ return self if block.call(self)
- leaf = @leaves.find { |l| block.call(l.content) }
+ leaf = @leaves.find { |l| block.call(l) }
return leaf if leaf
@children.each do |child|
node = child.find &block
return node if node
@@ -195,17 +199,17 @@
#
# return the visitor
#
def accept(visitor)
- visitor.enter_tree_node(self)
+ visitor.enter_node(self)
@leaves.each { |leaf|
leaf.accept(visitor)
}
@children.each { |child|
child.accept(visitor)
}
- visitor.exit_tree_node(self)
+ visitor.exit_node(self)
visitor
end
#
# Format the content of tree