Class: MakeMenu::MenuItemGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/make_menu/menu_item_group.rb

Overview

This class represents a group of menu items, with a title line

Constant Summary collapse

INDENT =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = 'Commands') ⇒ MenuItemGroup

Returns a new instance of MenuItemGroup.

Parameters:

  • title (String) (defaults to: 'Commands')

    The title text to display at the top of the group



11
12
13
14
# File 'lib/make_menu/menu_item_group.rb', line 11

def initialize(title = 'Commands')
  @title = title
  @items = []
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



16
17
18
# File 'lib/make_menu/menu_item_group.rb', line 16

def items
  @items
end

#titleObject (readonly)

Returns the value of attribute title.



16
17
18
# File 'lib/make_menu/menu_item_group.rb', line 16

def title
  @title
end

Instance Method Details

#add_item(item) ⇒ MenuItem

Add a new item to the group

Parameters:

Returns:



21
22
23
24
# File 'lib/make_menu/menu_item_group.rb', line 21

def add_item(item)
  items << item
  item
end

#heightInteger

Returns Number of rows needed to display the group.

Returns:

  • (Integer)

    Number of rows needed to display the group



32
33
34
# File 'lib/make_menu/menu_item_group.rb', line 32

def height
  items.size + 2
end

#to_sString

Returns Text representation of group.

Returns:

  • (String)

    Text representation of group



37
38
39
40
41
42
43
44
45
# File 'lib/make_menu/menu_item_group.rb', line 37

def to_s
  result = "#{' ' * INDENT}#{title}\n"

  items.each do |item|
    result += "#{item}\n"
  end

  "#{result} "
end

#widthInteger

Returns Number of characters needed to display the widest item.

Returns:

  • (Integer)

    Number of characters needed to display the widest item



27
28
29
# File 'lib/make_menu/menu_item_group.rb', line 27

def width
  [items.map(&:width).max, title.length + INDENT].max
end