Skip to content

Commit 797157d

Browse files
committed
first pass at resolving #20
1 parent 426f689 commit 797157d

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

lib/sgf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
$: << File.dirname(__FILE__)
22

3+
require 'observer'
34
require 'sgf/error'
45
require 'sgf/version'
56
require 'sgf/properties'

lib/sgf/node.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module SGF
22

33
#Your basic node. It holds information about itself, its parent, and its children.
44
class Node
5+
include Observable
56

67
attr_accessor :parent, :children, :properties, :depth
78

@@ -23,6 +24,9 @@ def add_children *nodes
2324
nodes.flatten!
2425
raise "Non-node child given!" if nodes.any? { |node| node.class != Node }
2526
nodes.each do |node|
27+
if node.parent && node.parent.children
28+
node.parent.children.delete node
29+
end
2630
node.parent = self
2731
node.depth = @depth + 1
2832
@children << node

spec/node_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,14 @@
9494
child.depth.should == 1
9595
end
9696

97+
it "should not be the child of many nodes" do
98+
parent1 = SGF::Node.new
99+
parent2 = SGF::Node.new
100+
parent1.add_children @node
101+
parent2.add_children @node
102+
@node.parent.should eq(parent2)
103+
parent2.children.should include(@node)
104+
parent1.children.should_not include(@node)
105+
end
106+
97107
end

0 commit comments

Comments
 (0)