Class: Redmine::MenuManager::MenuNode
- Inherits:
-
Object
- Object
- Redmine::MenuManager::MenuNode
- Includes:
- Enumerable
- Defined in:
- lib/redmine/menu_manager.rb
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.
360 361 362 363 364 |
# File 'lib/redmine/menu_manager.rb', line 360 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.
358 359 360 |
# File 'lib/redmine/menu_manager.rb', line 358 def last_items_count @last_items_count end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
358 359 360 |
# File 'lib/redmine/menu_manager.rb', line 358 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
357 358 359 |
# File 'lib/redmine/menu_manager.rb', line 357 def parent @parent end |
Instance Method Details
#add(child) ⇒ Object Also known as: <<
Adds a child
404 405 406 407 |
# File 'lib/redmine/menu_manager.rb', line 404 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
390 391 392 393 394 |
# File 'lib/redmine/menu_manager.rb', line 390 def add_at(child, position) @children.insert(position, child) child.parent = self child end |
#add_last(child) ⇒ Object
Adds a child as last child
397 398 399 400 401 |
# File 'lib/redmine/menu_manager.rb', line 397 def add_last(child) add_at(child, -1) @last_items_count += 1 child end |
#children ⇒ Object
366 367 368 369 370 371 372 |
# File 'lib/redmine/menu_manager.rb', line 366 def children if block_given? @children.each {|child| yield child} else @children end end |
#each {|_self| ... } ⇒ Object
379 380 381 382 |
# File 'lib/redmine/menu_manager.rb', line 379 def each(...) yield self children {|child| child.each(...)} end |
#position ⇒ Object
Returns the position for this node in it’s parent
419 420 421 |
# File 'lib/redmine/menu_manager.rb', line 419 def position self.parent.children.index(self) end |
#prepend(child) ⇒ Object
Adds a child at first position
385 386 387 |
# File 'lib/redmine/menu_manager.rb', line 385 def prepend(child) add_at(child, 0) end |
#remove!(child) ⇒ Object
Removes a child
411 412 413 414 415 416 |
# File 'lib/redmine/menu_manager.rb', line 411 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
424 425 426 427 428 |
# File 'lib/redmine/menu_manager.rb', line 424 def root root = self root = root.parent while root.parent root end |
#size ⇒ Object
Returns the number of descendants + 1
375 376 377 |
# File 'lib/redmine/menu_manager.rb', line 375 def size @children.inject(1) {|sum, node| sum + node.size} end |