Module: Flutterby::TreeWalker
Overview
A helper module with methods to walk across a node tree in various directions and variations and perform a block of code on each passed node.
Class Method Summary collapse
-
.walk_down(node, val = nil, &blk) ⇒ Object
Walk the graph from the root to the specified node.
-
.walk_tree(node, val = nil, &blk) ⇒ Object
Walk the entire tree, top to bottom, starting with its root, and then descending into its child layers.
-
.walk_up(node, val = nil, &blk) ⇒ Object
Walk the tree up, invoking the passed block for every node found on the way, passing the node as its only argument.
Instance Method Summary collapse
-
#walk_down(node, val = nil, &blk) ⇒ Object
Walk the graph from the root to the specified node.
-
#walk_tree(node, val = nil, &blk) ⇒ Object
Walk the entire tree, top to bottom, starting with its root, and then descending into its child layers.
-
#walk_up(node, val = nil, &blk) ⇒ Object
Walk the tree up, invoking the passed block for every node found on the way, passing the node as its only argument.
Class Method Details
.walk_down(node, val = nil, &blk) ⇒ Object
Walk the graph from the root to the specified node. Just like #walk_up, except the block will be called on higher level nodes first.
19 20 21 22 |
# File 'lib/flutterby/tree_walker.rb', line 19 def walk_down(node, val = nil, &blk) val = node.parent ? walk_up(node.parent, val, &blk) : val blk.call(node, val) end |
.walk_tree(node, val = nil, &blk) ⇒ Object
Walk the entire tree, top to bottom, starting with its root, and then descending into its child layers.
27 28 29 30 31 32 33 34 35 |
# File 'lib/flutterby/tree_walker.rb', line 27 def walk_tree(node, val = nil, &blk) val = blk.call(node, val) node.children.each do |child| val = walk_tree(child, val, &blk) end val end |
.walk_up(node, val = nil, &blk) ⇒ Object
Walk the tree up, invoking the passed block for every node found on the way, passing the node as its only argument.
11 12 13 14 |
# File 'lib/flutterby/tree_walker.rb', line 11 def walk_up(node, val = nil, &blk) val = blk.call(node, val) node.parent ? walk_up(node.parent, val, &blk) : val end |
Instance Method Details
#walk_down(node, val = nil, &blk) ⇒ Object
Walk the graph from the root to the specified node. Just like #walk_up, except the block will be called on higher level nodes first.
19 20 21 22 |
# File 'lib/flutterby/tree_walker.rb', line 19 def walk_down(node, val = nil, &blk) val = node.parent ? walk_up(node.parent, val, &blk) : val blk.call(node, val) end |
#walk_tree(node, val = nil, &blk) ⇒ Object
Walk the entire tree, top to bottom, starting with its root, and then descending into its child layers.
27 28 29 30 31 32 33 34 35 |
# File 'lib/flutterby/tree_walker.rb', line 27 def walk_tree(node, val = nil, &blk) val = blk.call(node, val) node.children.each do |child| val = walk_tree(child, val, &blk) end val end |
#walk_up(node, val = nil, &blk) ⇒ Object
Walk the tree up, invoking the passed block for every node found on the way, passing the node as its only argument.
11 12 13 14 |
# File 'lib/flutterby/tree_walker.rb', line 11 def walk_up(node, val = nil, &blk) val = blk.call(node, val) node.parent ? walk_up(node.parent, val, &blk) : val end |