Class: Redmine::MenuManager::MenuNode

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/redmine/menu_manager.rb

Direct Known Subclasses

MenuItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, content = nil) ⇒ MenuNode

Returns a new instance of MenuNode.



351
352
353
354
355
# File 'lib/redmine/menu_manager.rb', line 351

def initialize(name, content = nil)
  @name = name
  @children = []
  @last_items_count = 0
end

Instance Attribute Details

#last_items_countObject (readonly)

Returns the value of attribute last_items_count.



349
350
351
# File 'lib/redmine/menu_manager.rb', line 349

def last_items_count
  @last_items_count
end

#nameObject (readonly)

Returns the value of attribute name.



349
350
351
# File 'lib/redmine/menu_manager.rb', line 349

def name
  @name
end

#parentObject

Returns the value of attribute parent.



348
349
350
# File 'lib/redmine/menu_manager.rb', line 348

def parent
  @parent
end

Instance Method Details

#add(child) ⇒ Object Also known as: <<

Adds a child



397
398
399
400
# File 'lib/redmine/menu_manager.rb', line 397

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



381
382
383
384
385
386
387
# File 'lib/redmine/menu_manager.rb', line 381

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



390
391
392
393
394
# File 'lib/redmine/menu_manager.rb', line 390

def add_last(child)
  add_at(child, -1)
  @last_items_count += 1
  child
end

#childrenObject



357
358
359
360
361
362
363
# File 'lib/redmine/menu_manager.rb', line 357

def children
  if block_given?
    @children.each {|child| yield child}
  else
    @children
  end
end

#each {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



370
371
372
373
# File 'lib/redmine/menu_manager.rb', line 370

def each(&block)
  yield self
  children {|child| child.each(&block)}
end

#positionObject

Returns the position for this node in it’s parent



412
413
414
# File 'lib/redmine/menu_manager.rb', line 412

def position
  self.parent.children.index(self)
end

#prepend(child) ⇒ Object

Adds a child at first position



376
377
378
# File 'lib/redmine/menu_manager.rb', line 376

def prepend(child)
  add_at(child, 0)
end

#remove!(child) ⇒ Object

Removes a child



404
405
406
407
408
409
# File 'lib/redmine/menu_manager.rb', line 404

def remove!(child)
  @children.delete(child)
  @last_items_count -= +1 if child && child.last
  child.parent = nil
  child
end

#rootObject

Returns the root for this node



417
418
419
420
421
# File 'lib/redmine/menu_manager.rb', line 417

def root
  root = self
  root = root.parent while root.parent
  root
end

#sizeObject

Returns the number of descendants + 1



366
367
368
# File 'lib/redmine/menu_manager.rb', line 366

def size
  @children.inject(1) {|sum, node| sum + node.size}
end