Class: Redmine::MenuManager::MenuNode
- Inherits:
-
Object
- Object
- Redmine::MenuManager::MenuNode
- Includes:
- Enumerable
- Defined in:
- lib/redmine/menu_manager.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#last_items_count ⇒ Object
readonly
Returns the value of attribute last_items_count.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
-
#add(child) ⇒ Object
(also: #<<)
Adds a child.
-
#add_at(child, position) ⇒ Object
Adds a child at given position.
-
#add_last(child) ⇒ Object
Adds a child as last child.
- #children ⇒ Object
- #each {|_self| ... } ⇒ Object
-
#initialize(name, content = nil) ⇒ MenuNode
constructor
A new instance of MenuNode.
-
#position ⇒ Object
Returns the position for this node in it's parent.
-
#prepend(child) ⇒ Object
Adds a child at first position.
-
#remove!(child) ⇒ Object
Removes a child.
-
#root ⇒ Object
Returns the root for this node.
-
#size ⇒ Object
Returns the number of descendants + 1.
Constructor Details
#initialize(name, content = nil) ⇒ MenuNode
Returns a new instance of MenuNode
317 318 319 320 321 |
# File 'lib/redmine/menu_manager.rb', line 317 def initialize(name, content = nil) @name = name @children = [] @last_items_count = 0 end |
Instance Attribute Details
#last_items_count ⇒ Object (readonly)
Returns the value of attribute last_items_count
315 316 317 |
# File 'lib/redmine/menu_manager.rb', line 315 def last_items_count @last_items_count end |
#name ⇒ Object (readonly)
Returns the value of attribute name
315 316 317 |
# File 'lib/redmine/menu_manager.rb', line 315 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent
314 315 316 |
# File 'lib/redmine/menu_manager.rb', line 314 def parent @parent end |
Instance Method Details
#add(child) ⇒ Object Also known as: <<
Adds a child
363 364 365 366 |
# File 'lib/redmine/menu_manager.rb', line 363 def add(child) position = @children.size - @last_items_count add_at(child, position) end |
#add_at(child, position) ⇒ Object
Adds a child at given position
347 348 349 350 351 352 353 |
# File 'lib/redmine/menu_manager.rb', line 347 def add_at(child, position) raise "Child already added" if find {|node| node.name == child.name} @children = @children.insert(position, child) child.parent = self child end |
#add_last(child) ⇒ Object
Adds a child as last child
356 357 358 359 360 |
# File 'lib/redmine/menu_manager.rb', line 356 def add_last(child) add_at(child, -1) @last_items_count += 1 child end |
#children ⇒ Object
323 324 325 326 327 328 329 |
# File 'lib/redmine/menu_manager.rb', line 323 def children if block_given? @children.each {|child| yield child} else @children end end |
#each {|_self| ... } ⇒ Object
336 337 338 339 |
# File 'lib/redmine/menu_manager.rb', line 336 def each &block yield self children { |child| child.each(&block) } end |
#position ⇒ Object
Returns the position for this node in it's parent
378 379 380 |
# File 'lib/redmine/menu_manager.rb', line 378 def position self.parent.children.index(self) end |
#prepend(child) ⇒ Object
Adds a child at first position
342 343 344 |
# File 'lib/redmine/menu_manager.rb', line 342 def prepend(child) add_at(child, 0) end |
#remove!(child) ⇒ Object
Removes a child
370 371 372 373 374 375 |
# File 'lib/redmine/menu_manager.rb', line 370 def remove!(child) @children.delete(child) @last_items_count -= +1 if child && child.last child.parent = nil child end |
#root ⇒ Object
Returns the root for this node
383 384 385 386 387 |
# File 'lib/redmine/menu_manager.rb', line 383 def root root = self root = root.parent while root.parent root end |
#size ⇒ Object
Returns the number of descendants + 1
332 333 334 |
# File 'lib/redmine/menu_manager.rb', line 332 def size @children.inject(1) {|sum, node| sum + node.size} end |