basic_tree 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/basic_tree/version.rb +1 -1
- data/lib/basic_tree.rb +12 -3
- data/spec/lib/basic_tree_spec.rb +39 -0
- metadata +3 -3
data/lib/basic_tree/version.rb
CHANGED
data/lib/basic_tree.rb
CHANGED
@@ -3,6 +3,7 @@ require 'active_support/core_ext/object'
|
|
3
3
|
class BasicTree
|
4
4
|
|
5
5
|
include Enumerable
|
6
|
+
include Comparable
|
6
7
|
|
7
8
|
class Kids < Array
|
8
9
|
def swap!(p1, p2)
|
@@ -16,7 +17,9 @@ class BasicTree
|
|
16
17
|
def initialize(object, parent = nil, &block)
|
17
18
|
self.object = object
|
18
19
|
parent.try(:insert!, self)
|
19
|
-
|
20
|
+
if block_given?
|
21
|
+
block.arity == 1 ? yield(self) : instance_eval(&block)
|
22
|
+
end
|
20
23
|
end
|
21
24
|
|
22
25
|
# TODO: test
|
@@ -35,7 +38,7 @@ class BasicTree
|
|
35
38
|
kids << basic_tree
|
36
39
|
end
|
37
40
|
|
38
|
-
# TODO:
|
41
|
+
# TODO: test
|
39
42
|
def remove!(basic_tree)
|
40
43
|
raise ArgumentError, "Must be a #{self.class}" unless basic_tree.is_a?(self.class)
|
41
44
|
raise StandardError, "Can't remove root" if root?
|
@@ -84,7 +87,7 @@ class BasicTree
|
|
84
87
|
end
|
85
88
|
|
86
89
|
def siblings
|
87
|
-
root? ? [] : siblings_and_self.delete_if { |s| s
|
90
|
+
root? ? [] : siblings_and_self.delete_if { |s| s.equal?(self) }
|
88
91
|
end
|
89
92
|
|
90
93
|
##################################################
|
@@ -122,6 +125,12 @@ class BasicTree
|
|
122
125
|
root? || siblings_and_self.last == self
|
123
126
|
end
|
124
127
|
|
128
|
+
##################################################
|
129
|
+
|
130
|
+
def <=>(other)
|
131
|
+
subtree.map(&:object) <=> other.subtree.map(&:object)
|
132
|
+
end
|
133
|
+
|
125
134
|
##################################################
|
126
135
|
private
|
127
136
|
|
data/spec/lib/basic_tree_spec.rb
CHANGED
@@ -16,6 +16,16 @@ describe BasicTree do
|
|
16
16
|
@a12 = @a1.children[1]
|
17
17
|
end
|
18
18
|
|
19
|
+
it "allows for a different initialization syntax" do
|
20
|
+
@a.should == BasicTree.new("a") do |t|
|
21
|
+
t.add "a1" do |t|
|
22
|
+
t.add "a11"
|
23
|
+
t.add "a12"
|
24
|
+
end
|
25
|
+
t.add "a2"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
19
29
|
describe "instance methods" do
|
20
30
|
before do
|
21
31
|
@object = "soccer"
|
@@ -147,6 +157,35 @@ describe BasicTree do
|
|
147
157
|
end
|
148
158
|
end
|
149
159
|
end
|
160
|
+
|
161
|
+
describe "#<=>" do
|
162
|
+
before do
|
163
|
+
@t1 = BasicTree.new "a" do
|
164
|
+
add "a1"
|
165
|
+
add "a2"
|
166
|
+
end
|
167
|
+
@t2 = BasicTree.new "a" do
|
168
|
+
add "a1"
|
169
|
+
add "a2"
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context "when comparing the subtrees returns a 0" do
|
174
|
+
it { (@t1 <=> @t2).should eq(0) }
|
175
|
+
end
|
176
|
+
|
177
|
+
context "when comparing the subtrees returns a 1" do
|
178
|
+
before { @t2 = BasicTree.new("a") { add "a1" } }
|
179
|
+
|
180
|
+
it { (@t1 <=> @t2).should eq(1) }
|
181
|
+
end
|
182
|
+
|
183
|
+
context "when comparing the subtrees returns a -1" do
|
184
|
+
before { @t2.add "a3" }
|
185
|
+
|
186
|
+
it { (@t1 <=> @t2).should eq(-1) }
|
187
|
+
end
|
188
|
+
end
|
150
189
|
end
|
151
190
|
|
152
191
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 1.0.
|
8
|
+
- 2
|
9
|
+
version: 1.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Austin Schneider
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-03-
|
17
|
+
date: 2011-03-18 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|