Code cleanup.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8229 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2011-12-15 10:57:59 +00:00
parent 77e8a76af3
commit f6dd3c5484
1 changed files with 7 additions and 24 deletions

View File

@ -316,7 +316,6 @@ module Redmine
def initialize(name, content = nil) def initialize(name, content = nil)
@name = name @name = name
@childrenHash ||= {}
@children = [] @children = []
@last_items_count = 0 @last_items_count = 0
end end
@ -341,50 +340,34 @@ module Redmine
# Adds a child at first position # Adds a child at first position
def prepend(child) def prepend(child)
raise "Child already added" if @childrenHash.has_key?(child.name) add_at(child, 0)
@childrenHash[child.name] = child
@children = [child] + @children
child.parent = self
return child
end end
# Adds a child at given position # Adds a child at given position
def add_at(child, position) def add_at(child, position)
raise "Child already added" if @childrenHash.has_key?(child.name) raise "Child already added" if find {|node| node.name == child.name}
@childrenHash[child.name] = child
@children = @children.insert(position, child) @children = @children.insert(position, child)
child.parent = self child.parent = self
return child child
end end
# Adds a child as last child # Adds a child as last child
def add_last(child) def add_last(child)
raise "Child already added" if @childrenHash.has_key?(child.name) add_at(child, -1)
@childrenHash[child.name] = child
@children << child
@last_items_count += 1 @last_items_count += 1
child.parent = self child
return child
end end
# Adds a child # Adds a child
def add(child) def add(child)
raise "Child already added" if @childrenHash.has_key?(child.name)
@childrenHash[child.name] = child
position = @children.size - @last_items_count position = @children.size - @last_items_count
@children.insert(position, child) add_at(child, position)
child.parent = self
return child
end end
alias :<< :add alias :<< :add
# Removes a child # Removes a child
def remove!(child) def remove!(child)
@childrenHash.delete(child.name)
@children.delete(child) @children.delete(child)
@last_items_count -= +1 if child && child.last @last_items_count -= +1 if child && child.last
child.parent = nil child.parent = nil