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.



355
356
357
358
359
# File 'lib/redmine/menu_manager.rb', line 355

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.



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

def last_items_count
  @last_items_count
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

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

Adds a child



401
402
403
404
# File 'lib/redmine/menu_manager.rb', line 401

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



385
386
387
388
389
390
391
# File 'lib/redmine/menu_manager.rb', line 385

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



394
395
396
397
398
# File 'lib/redmine/menu_manager.rb', line 394

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

#childrenObject



361
362
363
364
365
366
367
# File 'lib/redmine/menu_manager.rb', line 361

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

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

Yields:

  • (_self)

Yield Parameters:



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

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

#positionObject

Returns the position for this node in it’s parent



416
417
418
# File 'lib/redmine/menu_manager.rb', line 416

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

#prepend(child) ⇒ Object

Adds a child at first position



380
381
382
# File 'lib/redmine/menu_manager.rb', line 380

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

#remove!(child) ⇒ Object

Removes a child



408
409
410
411
412
413
# File 'lib/redmine/menu_manager.rb', line 408

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



421
422
423
424
425
# File 'lib/redmine/menu_manager.rb', line 421

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

#sizeObject

Returns the number of descendants + 1



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

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