redwood 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -61,8 +61,11 @@ Methods include:
61
61
  descendants ## All of the descendant nodes of this node.
62
62
  depth ## Integer representing how deep this node is in the tree.
63
63
  ## A root node has a depth of 1, its children: 2, etc.
64
+ height ## The length of this node to its furthest descendant.
65
+ ## A leaf node has a height of 1.
64
66
  unlink ## Detach this node from its parent.
65
67
  prune ## Unlink all of this node's chidren.
68
+ graft ## Add a node to this node's children.
66
69
  walk ## Recursively yield every node in this tree to a block
67
70
  view ## Make a fancy string representation of the tree
68
71
  ## as seen in the command-line tool
@@ -73,7 +76,7 @@ The Redwood::Node class is a simple implementation of the Redwood module. It is
73
76
 
74
77
  add_child(name) ## Add a child node. Nodes can have a #name.
75
78
  [](name) ## Lookup children node by their #name.
76
- <<(node) ## Add a node to this node's children.
79
+ <<(node) ## Alias for `graft`.
77
80
 
78
81
  #### Redwood::FileNode
79
82
 
@@ -4,7 +4,7 @@
4
4
  # also mixes in tree-like methods. See Redwood::Node for the canononical representation.
5
5
 
6
6
  module Redwood
7
- VERSION = "0.1.1"
7
+ VERSION = "0.1.2"
8
8
 
9
9
  # This node's parent.
10
10
  def parent
@@ -82,6 +82,17 @@ module Redwood
82
82
  ancestors.size + 1
83
83
  end
84
84
 
85
+ # From Wikipedia: The height of a node is the length
86
+ # of the longest downward path to a leaf from that node.
87
+ # In other words, the length of this node to its furthest descendant.
88
+ def height
89
+ if !leaf?
90
+ descendants.collect {|child| child.depth }.uniq.size + 1
91
+ else
92
+ 1
93
+ end
94
+ end
95
+
85
96
  # Orphan this node. Remove it from its parent node.
86
97
  def unlink
87
98
  if parent
@@ -98,6 +109,13 @@ module Redwood
98
109
  end
99
110
  end
100
111
 
112
+ # Append a node to this node's children, and return the node.
113
+ def graft(node)
114
+ node.instance_variable_set(:@parent, self)
115
+ children << node
116
+ node
117
+ end
118
+
101
119
  # Recursively yield every node in the tree.
102
120
  def walk(&block)
103
121
  if block_given?
@@ -20,11 +20,9 @@ module Redwood
20
20
  child
21
21
  end
22
22
 
23
- # Add a node to this nodes children, and return the child
23
+ # Graft a child
24
24
  def <<(child)
25
- child.instance_variable_set(:@parent, self)
26
- children << child
27
- child
25
+ graft child
28
26
  end
29
27
 
30
28
  # Lookup a child node by its name
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{redwood}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mark Wunsch"]
12
- s.date = %q{2010-04-12}
12
+ s.date = %q{2010-04-14}
13
13
  s.default_executable = %q{redwood}
14
14
  s.description = %q{A simple library to create and manage basic tree-esque structures.}
15
15
  s.email = ["mark@markwunsch.com"]
@@ -19,8 +19,7 @@ Gem::Specification.new do |s|
19
19
  "README.md"
20
20
  ]
21
21
  s.files = [
22
- ".document",
23
- ".gitignore",
22
+ ".gitignore",
24
23
  "Gemfile",
25
24
  "Gemfile.lock",
26
25
  "LICENSE",
@@ -109,6 +109,18 @@ class TestRedwood < Test::Unit::TestCase
109
109
  assert_equal 4, greatgrandson.depth
110
110
  end
111
111
 
112
+ test 'has a height' do
113
+ node = Redwood::Node.new(:parent)
114
+ son = node.add_child(:son)
115
+ daughter = node.add_child(:daughter)
116
+ grandson = son.add_child(:grandson)
117
+ greatgrandson = grandson.add_child(:greatgrandson)
118
+
119
+ assert_equal greatgrandson.depth, node.height
120
+ assert_equal 1, daughter.height
121
+ assert_equal son.height, (node.height - 1)
122
+ end
123
+
112
124
  test 'has a treeview' do
113
125
  node = Redwood::Node.new(:parent)
114
126
  dog = node.add_child(:dog)
@@ -141,6 +153,16 @@ class TestRedwood < Test::Unit::TestCase
141
153
  assert_equal 6, counter
142
154
  end
143
155
 
156
+ test 'grafts a node' do
157
+ node = Redwood::Node.new(:parent)
158
+ dog = Redwood::Node.new(:dog)
159
+
160
+ node.graft dog
161
+
162
+ assert_equal node, dog.parent
163
+ assert node.children.include?(dog)
164
+ end
165
+
144
166
  test 'add a child with the << method' do
145
167
  node = Redwood::Node.new(:parent)
146
168
  dog = Redwood::Node.new(:dog)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mark Wunsch
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-12 00:00:00 -04:00
17
+ date: 2010-04-14 00:00:00 -04:00
18
18
  default_executable: redwood
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -42,7 +42,6 @@ extra_rdoc_files:
42
42
  - LICENSE
43
43
  - README.md
44
44
  files:
45
- - .document
46
45
  - .gitignore
47
46
  - Gemfile
48
47
  - Gemfile.lock
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE