Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a node to be renamed #35

Merged
merged 1 commit into from
Dec 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class TreeNode

# @!group Core Attributes

# @!attribute [r] name
# @!attribute [rw] name
#
# Name of this node. Expected to be unique within the tree.
#
Expand All @@ -105,8 +105,12 @@ class TreeNode
# retain unique names within the tree structure, and use the
# +content+ attribute for any non-unique node requirements.
#
# If you want to change the name, you probably want to call +rename+
# instead.
#
# @see content
attr_reader :name
# @see rename
attr_accessor :name

# @!attribute [rw] content
# Content of this node. Can be +nil+. Note that there is no
Expand Down Expand Up @@ -381,6 +385,35 @@ def insertion_range

private :insertion_range

# Renames the node and updates the parent's reference to it
#
# @param [Object] name Name of the node. Conventional usage is to pass a String
# (Integer names may cause *surprises*)
#
# @return [Object] The old name
def rename(new_name)
old_name = @name

if is_root?
@name = new_name
else
@parent.rename_child @name, new_name
end

old_name
end

# Renames the specified child node
#
# @param [Object] name old Name of the node. Conventional usage is to pass
# a String (Integer names may cause *surprises*)
#
# @param [Object] name new Name of the node. Conventional usage is to pass
# a String (Integer names may cause *surprises*)
def rename_child(old_name, new_name)
@children_hash[new_name] = @children_hash.delete(old_name)
@children_hash[new_name].name= new_name
end

# Replaces the specified child node with another child node on this node.
#
Expand Down
21 changes: 21 additions & 0 deletions test/test_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,27 @@ def test_merge_bang
assert_raise(TypeError) { @root.merge!('ROOT') }
end

def test_name_accessor
setup_test_tree

assert_equal 'ROOT', @root.name, "Name should be 'ROOT'"

@root.name = 'ROOT2'

assert_equal 'ROOT2', @root.name, "Name should be 'ROOT2'"
end

def test_renam
setup_test_tree

@root.rename 'ALT_ROOT'
assert_equal('ALT_ROOT', @root.name, "Name should be 'ALT_ROOT'")

@child1.rename 'ALT_Child1'
assert_equal('ALT_Child1', @child1.name)
assert_equal('ALT_Child1', @child1.name, "Name should be 'ALT_Child1'")
assert_equal(@child1, @root['ALT_Child1'], 'Should be able to access from parent using new name')
end
end
end

Expand Down