lib/redwood.rb in redwood-0.0.1 vs lib/redwood.rb in redwood-0.1.0
- old
+ new
@@ -1,96 +1,122 @@
-# The Redood module can be mixed in to give tree-like methods to an object.
+# The Redwood module can be mixed in to give tree-like methods to an object.
# Its primary implementation is the Redwood::Node class. Its only requirement
# is that the tree-infused-object's children and parent is an object that
-# also mixes in Redwood. See Redwood::Node for the canononical representation.
+# also mixes in tree-like methods. See Redwood::Node for the canononical representation.
module Redwood
- VERSION = "0.0.1"
+ VERSION = "0.1.0"
+ # This node's parent.
def parent
@parent
end
- def name
- end
-
- def value
- end
-
+ # Is this node the root of the tree?
def root?
parent.nil?
end
+ # Is this node a leaf node? Is this node childless?
def leaf?
- children.empty?
+ children.nil? || children.empty?
end
+ # Get the root node in this tree.
def root
if root?
self
else
parent.root
end
end
-
+
+ # Get the children of this node.
def children
@children ||= []
end
+ # Get the siblings of this node. The other children belonging to this node's parent.
def siblings
if parent
parent.children.reject {|child| child == self }
end
end
+ # Is this node the only child of its parent. Does it have any siblings?
def only_child?
if parent
siblings.empty?
end
end
- def childless?
- children.nil? || children.empty?
- end
-
+ # Does this node have children? Is it not a leaf node?
def has_children?
- !childless?
+ !leaf?
end
-
+
+ # Get all of the ancestors for this node.
def ancestors
- return @ancestors if @ancestors
- @ancestors = []
+ ancestors = []
if parent
- @ancestors << parent
- parent.ancestors.each {|ancestor| @ancestors << ancestor }
+ ancestors << parent
+ parent.ancestors.each {|ancestor| ancestors << ancestor }
end
- @ancestors
+ ancestors
end
-
+
+ # Get all of the descendants of this node.
+ # All of its children, and its childrens' children, and its childrens' childrens' children...
def descendants
- return @descendants if @descendants
- @descendants = []
+ descendants = []
if !children.empty?
- (@descendants << children).flatten!
- children.each {|descendant| @descendants << descendant.descendants }
- @descendants.flatten!
+ (descendants << children).flatten!
+ children.each {|descendant| descendants << descendant.descendants }
+ descendants.flatten!
end
- @descendants
+ descendants
end
-
+
+ # An integer representation of how deep in the tree this node is.
+ # The root node has a depth of 1, its children have a depth of 2, etc.
def depth
ancestors.size + 1
end
+ # Orphan this node. Remove it from its parent node.
+ def unlink
+ if parent
+ parent.children.delete(self)
+ self.instance_variable_set(:@parent, nil)
+ return self
+ end
+ end
+
+ # Abandon all of this node's children.
+ def prune
+ if children
+ children.each {|child| child.unlink }
+ end
+ end
+
+ # Recursively yield every node in the tree.
+ def walk(&block)
+ if block_given?
+ yield self
+ children.each {|child| child.walk(&block) }
+ end
+ end
+
+ # Makes a pretty string representation of the tree.
def view(content = :name)
treeview = ''
if parent
treeview += parent.children.last.eql?(self) ? "`" : "|"
treeview << "--\s"
end
treeview << "#{self.send(content)}"
- if !children.empty?
+ if has_children?
treeview << "\n"
children.each do |child|
if parent
child.ancestors.reverse_each do |ancestor|
if !ancestor.root?
@@ -107,8 +133,8 @@
end
end
treeview
end
-end
-
-require 'redwood/node'
+ autoload :FileNode, 'redwood/filenode'
+ autoload :Node, 'redwood/node'
+end
\ No newline at end of file