Class: Redmine::MenuManager::MenuNode

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

Overview

Since:

  • 1.4.0

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



340
341
342
343
344
# File 'lib/redmine/menu_manager.rb', line 340

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



338
339
340
# File 'lib/redmine/menu_manager.rb', line 338

def last_items_count
  @last_items_count
end

#nameObject (readonly)

Returns the value of attribute name



338
339
340
# File 'lib/redmine/menu_manager.rb', line 338

def name
  @name
end

#parentObject

Returns the value of attribute parent



337
338
339
# File 'lib/redmine/menu_manager.rb', line 337

def parent
  @parent
end

Instance Method Details

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

Adds a child



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

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



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

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



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

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

#childrenObject



346
347
348
349
350
351
352
# File 'lib/redmine/menu_manager.rb', line 346

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

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

Yields:

  • (_self)

Yield Parameters:



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

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

#positionObject

Returns the position for this node in it's parent



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

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

#prepend(child) ⇒ Object

Adds a child at first position



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

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

#remove!(child) ⇒ Object

Removes a child



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

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



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

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

#sizeObject

Returns the number of descendants + 1



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

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