Module: Redwood

Included in:
Node
Defined in:
lib/redwood.rb,
lib/redwood/node.rb,
lib/redwood/filenode.rb

Overview

Redwood::FileNode stores a Directory tree in a Redwood structure. FileNodes respond to methods of the File class. eg. FileNode#chmod, FileNode#stat, etc.

Defined Under Namespace

Classes: FileNode, Node

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#ancestorsObject

Get all of the ancestors for this node.



58
59
60
61
62
63
64
65
# File 'lib/redwood.rb', line 58

def ancestors
  ancestors = []
  if parent
    ancestors << parent
    parent.ancestors.each {|ancestor| ancestors << ancestor }
  end
  ancestors
end

#childrenObject

Get the children of this node.



34
35
36
# File 'lib/redwood.rb', line 34

def children
  @children ||= []
end

#depthObject

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.



81
82
83
# File 'lib/redwood.rb', line 81

def depth
  ancestors.size + 1
end

#descendantsObject

Get all of the descendants of this node. All of its children, and its childrens’ children, and its childrens’ childrens’ children…



69
70
71
72
73
74
75
76
77
# File 'lib/redwood.rb', line 69

def descendants
  descendants = []
  if !children.empty?
    (descendants << children).flatten!
    children.each {|descendant| descendants << descendant.descendants }
    descendants.flatten!   
  end
  descendants
end

#graft(node) ⇒ Object

Append a node to this node’s children, and return the node.



113
114
115
116
117
# File 'lib/redwood.rb', line 113

def graft(node)
  node.instance_variable_set(:@parent, self)
  children << node
  node
end

#has_children?Boolean

Does this node have children? Is it not a leaf node?

Returns:

  • (Boolean)


53
54
55
# File 'lib/redwood.rb', line 53

def has_children?
  !leaf?
end

#heightObject

From Wikipedia: The height of a node is the length of the longest downward path to a leaf from that node. In other words, the length of this node to its furthest descendant.



88
89
90
91
92
93
94
# File 'lib/redwood.rb', line 88

def height
  if !leaf?
    descendants.collect {|child| child.depth }.uniq.size + 1
  else
    1
  end
end

#leaf?Boolean

Is this node a leaf node? Is this node childless?

Returns:

  • (Boolean)


20
21
22
# File 'lib/redwood.rb', line 20

def leaf?
  children.nil? || children.empty?
end

#only_child?Boolean

Is this node the only child of its parent. Does it have any siblings?

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/redwood.rb', line 46

def only_child?
  if parent
    siblings.empty?
  end
end

#parentObject

This node’s parent.



10
11
12
# File 'lib/redwood.rb', line 10

def parent
  @parent
end

#pruneObject

Abandon all of this node’s children.



106
107
108
109
110
# File 'lib/redwood.rb', line 106

def prune
  if children
    children.each {|child| child.unlink }
  end
end

#rootObject

Get the root node in this tree.



25
26
27
28
29
30
31
# File 'lib/redwood.rb', line 25

def root
  if root?
    self
  else
    parent.root
  end
end

#root?Boolean

Is this node the root of the tree?

Returns:

  • (Boolean)


15
16
17
# File 'lib/redwood.rb', line 15

def root?
  parent.nil?
end

#siblingsObject

Get the siblings of this node. The other children belonging to this node’s parent.



39
40
41
42
43
# File 'lib/redwood.rb', line 39

def siblings
  if parent
    parent.children.reject {|child| child == self }
  end
end

Orphan this node. Remove it from its parent node.



97
98
99
100
101
102
103
# File 'lib/redwood.rb', line 97

def unlink
  if parent
    parent.children.delete(self)
    self.instance_variable_set(:@parent, nil)
    return self
  end
end

#view(content = :name) ⇒ Object

Makes a pretty string representation of the tree.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/redwood.rb', line 128

def view(content = :name)
  treeview = ''
  if parent
    treeview += parent.children.last.eql?(self) ? "`" : "|"
    treeview << "--\s"
  end
  treeview << "#{self.send(content)}"
  if has_children?
    treeview << "\n"
    children.each do |child|
      if parent
        child.ancestors.reverse_each do |ancestor|
          if !ancestor.root?
            if ancestor.only_child? || ancestor.parent.children.last.eql?(ancestor)
              treeview << "\s\s\s\s"
            else
              treeview << "|\s\s\s"
            end                
          end
        end
      end
      treeview << child.view(content)
      treeview << "\n" if !children.last.eql?(child)          
    end
  end
  treeview
end

#walk(&block) ⇒ Object

Recursively yield every node in the tree.



120
121
122
123
124
125
# File 'lib/redwood.rb', line 120

def walk(&block)
  if block_given?
    yield self
    children.each {|child| child.walk(&block) }
  end
end