module Rooted::Mutable
This module contains methods for mutating a tree node.
Public Instance Methods
Insert a child after the last one.
@param value [Object] the value of the new sibling. @return [self]
# File lib/rooted/mutable.rb, line 36 def append_child(value = nil) push value end
Insert a child between this node and the one after it.
@raise [StructureException] if this node has no parent.
@param value [Object] the value of the new sibling. @return [self]
# File lib/rooted/mutable.rb, line 12 def append_sibling(value = nil) raise StructureException, 'Root node can not have siblings' if root? append value self end
Removes the node from the tree.
@return [Array<Node>] an array of the children to the deleted node, now
made roots.
# File lib/rooted/mutable.rb, line 65 def delete extract.children.to_a.each(&:extract) end
Extracts the node and its subtree from the larger structure.
@return [self] the node will now be root.
# File lib/rooted/mutable.rb, line 54 def extract return self if root? method(:delete).super_method.call self end
Insert a child before the first one.
@param value [Object] the value of the new sibling. @return [self]
# File lib/rooted/mutable.rb, line 47 def prepend_child(value = nil) unshift value end
Insert a child between this node and the one before it.
@raise [StructureException] if this node has no parent.
@param value [Object] the value of the new sibling. @return [self]
# File lib/rooted/mutable.rb, line 25 def prepend_sibling(value = nil) raise StructureException, 'Root node can not have siblings' if root? prepend value self end