Skip to content

Commit

Permalink
Merge 253d628 into 0a1342c
Browse files Browse the repository at this point in the history
  • Loading branch information
aidansteele committed Dec 18, 2014
2 parents 0a1342c + 253d628 commit 8f7145f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
7 changes: 4 additions & 3 deletions lib/tree.rb
Expand Up @@ -140,7 +140,7 @@ def root
#
# @return [Boolean] +true+ if this is a root node.
def is_root?
@parent == nil
@parent.nil?
end

# @!attribute [r] has_content?
Expand Down Expand Up @@ -360,7 +360,7 @@ def <<(child)
def add(child, at_index = -1)
raise ArgumentError, "Attempting to add a nil node" unless child # Only handles the immediate child scenario
raise ArgumentError, "Attempting add node to itself" if self.equal?(child)
raise ArgumentError, "Attempting add root as a child" if self.equal?(root) unless self.is_root?
raise ArgumentError, "Attempting add root as a child" if child.equal?(root)

# Lazy mans unique test, won't test if children of child are unique in this tree too.
raise "Child #{child.name} already added!" if @children_hash.include?(child.name)
Expand Down Expand Up @@ -482,6 +482,7 @@ def remove!(child)
# @return [Tree::TreeNode] The parent node.
def parent=(parent) # :nodoc:
@parent = parent
@node_depth = nil
end

protected :parent=, :name=
Expand Down Expand Up @@ -516,7 +517,7 @@ def remove_all!
#
# @return +nil+.
def set_as_root! # :nodoc:
@parent = nil
self.parent = nil
end

protected :set_as_root!
Expand Down
3 changes: 1 addition & 2 deletions lib/tree/utils/metrics_methods.rb
Expand Up @@ -94,8 +94,7 @@ def node_height
#
# @return [Integer] Depth of this node.
def node_depth
return 0 if is_root?
1 + parent.node_depth
@node_depth ||= is_root? ? 0 : (1 + parent.node_depth)
end

# @!attribute [r] level
Expand Down
6 changes: 1 addition & 5 deletions test/test_tree.rb
Expand Up @@ -1470,11 +1470,7 @@ def test_add_node_to_self_as_child

# And now a scenario where the node addition is done down the hierarchy
child = Tree::TreeNode.new("child")
begin
root << child << root
rescue ArgumentError => e
fail("The ArgumentError should not have been raised.")
end
assert_raise(ArgumentError) { root << child << root }
end

# Test whether the tree_leaf method works correctly
Expand Down

0 comments on commit 8f7145f

Please sign in to comment.