tooled 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/tooled.rb +2 -0
- data/lib/tooled/tree_node.rb +48 -0
- data/lib/tooled/tree_node_collection.rb +14 -0
- data/lib/tooled/version.rb +1 -1
- data/spec/tree_node_collection_spec.rb +18 -0
- data/spec/tree_node_spec.rb +66 -0
- metadata +8 -2
data/lib/tooled.rb
CHANGED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "tooled/tree_node_collection"
|
2
|
+
|
3
|
+
module Tooled
|
4
|
+
class TreeNode
|
5
|
+
def initialize
|
6
|
+
@children = TreeNodeCollection.new
|
7
|
+
parent = nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def children
|
11
|
+
@children
|
12
|
+
end
|
13
|
+
|
14
|
+
def each_child
|
15
|
+
return to_enum(:each_child) unless block_given?
|
16
|
+
children.each do |child|
|
17
|
+
yield child
|
18
|
+
child.each_child { |c| yield c }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def parent
|
23
|
+
@parent
|
24
|
+
end
|
25
|
+
|
26
|
+
def descendants
|
27
|
+
enum = to_enum(:each_child)
|
28
|
+
TreeNodeCollection.new(enum.respond_to?(:lazy) ? enum.lazy : enum)
|
29
|
+
end
|
30
|
+
|
31
|
+
def ancestors
|
32
|
+
enum = ::Enumerator.new do |yielder|
|
33
|
+
parent_node = parent
|
34
|
+
while parent_node
|
35
|
+
yielder << parent_node
|
36
|
+
parent_node = parent_node.parent
|
37
|
+
end
|
38
|
+
end
|
39
|
+
TreeNodeCollection.new(enum.respond_to?(:lazy) ? enum.lazy : enum)
|
40
|
+
end
|
41
|
+
|
42
|
+
def parent=(node)
|
43
|
+
raise InvalidNodeException.new("`parent' can only be a TreeNode") unless node.is_a?(TreeNode)
|
44
|
+
raise InvalidNodeException.new("`parent' can not be a descendant") if descendants.member?(node)
|
45
|
+
@parent = node.tap { |n| n.children.add(self) }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "tooled/tree_node"
|
2
|
+
|
3
|
+
module Tooled
|
4
|
+
class InvalidNodeException < Exception; end
|
5
|
+
|
6
|
+
class TreeNodeCollection < ::Set
|
7
|
+
def add(node)
|
8
|
+
raise InvalidNodeException.new("Only TreeNode objects can be added to collection") unless node.is_a?(TreeNode)
|
9
|
+
@hash[node] = true
|
10
|
+
self
|
11
|
+
end
|
12
|
+
alias_method :<<, :add
|
13
|
+
end
|
14
|
+
end
|
data/lib/tooled/version.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "tooled/tree_node_collection"
|
2
|
+
|
3
|
+
describe Tooled::TreeNodeCollection do
|
4
|
+
let(:object) { Tooled::TreeNodeCollection.new }
|
5
|
+
subject { object }
|
6
|
+
|
7
|
+
describe "add" do
|
8
|
+
context "TreeNode to collection" do
|
9
|
+
specify { expect { object.add(Tooled::TreeNode.new) }.to change { object.size }.by(1) }
|
10
|
+
specify { expect { object << Tooled::TreeNode.new }.to change { object.size }.by(1) }
|
11
|
+
end
|
12
|
+
|
13
|
+
context "raises if not provided a TreeNode" do
|
14
|
+
specify { expect { object.add(String.new) }.to raise_error(Tooled::InvalidNodeException) }
|
15
|
+
specify { expect { object << String.new }.to raise_error(Tooled::InvalidNodeException) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require "tooled/tree_node"
|
2
|
+
|
3
|
+
describe Tooled::TreeNode do
|
4
|
+
let(:object) { Tooled::TreeNode.new }
|
5
|
+
subject { object }
|
6
|
+
|
7
|
+
its(:children) { should be_instance_of(Tooled::TreeNodeCollection) }
|
8
|
+
|
9
|
+
describe "reports all descendants" do
|
10
|
+
subject { object.descendants }
|
11
|
+
|
12
|
+
before do
|
13
|
+
object.children << Tooled::TreeNode.new
|
14
|
+
object.children << Tooled::TreeNode.new
|
15
|
+
object.children << Tooled::TreeNode.new.tap do |node_a|
|
16
|
+
node_a.children << Tooled::TreeNode.new.tap do |node_b|
|
17
|
+
node_b.children << Tooled::TreeNode.new
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it { should be_instance_of(Tooled::TreeNodeCollection) }
|
23
|
+
its(:size) { should eql(5) }
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "reports all ancestors" do
|
27
|
+
subject { object.ancestors }
|
28
|
+
|
29
|
+
before do
|
30
|
+
object.parent = Tooled::TreeNode.new.tap do |node_a|
|
31
|
+
node_a.parent = Tooled::TreeNode.new.tap do |node_b|
|
32
|
+
node_b.parent = Tooled::TreeNode.new.tap do |node_c|
|
33
|
+
node_c.parent = Tooled::TreeNode.new
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it { should be_instance_of(Tooled::TreeNodeCollection) }
|
40
|
+
its(:size) { should eql(4) }
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "setting the parent" do
|
44
|
+
context "to an invalid object" do
|
45
|
+
specify { expect { object.parent = String.new }.to raise_error(Tooled::InvalidNodeException) }
|
46
|
+
end
|
47
|
+
|
48
|
+
context "to a TreeNode" do
|
49
|
+
let(:parent) { Tooled::TreeNode.new }
|
50
|
+
before { object.parent = parent }
|
51
|
+
|
52
|
+
it "adds self to parents children" do
|
53
|
+
parent.children.to_a.should include(object)
|
54
|
+
end
|
55
|
+
|
56
|
+
its(:parent) { should eql(parent) }
|
57
|
+
end
|
58
|
+
|
59
|
+
context "to a child" do
|
60
|
+
let(:child) { Tooled::TreeNode.new }
|
61
|
+
before { object.children.add(child) }
|
62
|
+
|
63
|
+
specify { expect { object.parent = child }.to raise_error(Tooled::InvalidNodeException) }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tooled
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -76,12 +76,16 @@ files:
|
|
76
76
|
- lib/tooled/publisher.rb
|
77
77
|
- lib/tooled/queue.rb
|
78
78
|
- lib/tooled/stack.rb
|
79
|
+
- lib/tooled/tree_node.rb
|
80
|
+
- lib/tooled/tree_node_collection.rb
|
79
81
|
- lib/tooled/vector2d.rb
|
80
82
|
- lib/tooled/vector3d.rb
|
81
83
|
- lib/tooled/version.rb
|
82
84
|
- spec/publisher_spec.rb
|
83
85
|
- spec/queue_spec.rb
|
84
86
|
- spec/stack_spec.rb
|
87
|
+
- spec/tree_node_collection_spec.rb
|
88
|
+
- spec/tree_node_spec.rb
|
85
89
|
- spec/vector2d_spec.rb
|
86
90
|
- spec/vector3d_spec.rb
|
87
91
|
- tooled.gemspec
|
@@ -114,5 +118,7 @@ test_files:
|
|
114
118
|
- spec/publisher_spec.rb
|
115
119
|
- spec/queue_spec.rb
|
116
120
|
- spec/stack_spec.rb
|
121
|
+
- spec/tree_node_collection_spec.rb
|
122
|
+
- spec/tree_node_spec.rb
|
117
123
|
- spec/vector2d_spec.rb
|
118
124
|
- spec/vector3d_spec.rb
|