Class: MakeMenu::MenuItemGroup
- Inherits:
-
Object
- Object
- MakeMenu::MenuItemGroup
- 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
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
-
#add_item(item) ⇒ MenuItem
Add a new item to the group.
-
#height ⇒ Integer
Number of rows needed to display the group.
-
#initialize(title = 'Commands') ⇒ MenuItemGroup
constructor
A new instance of MenuItemGroup.
-
#to_s ⇒ String
Text representation of group.
-
#width ⇒ Integer
Number of characters needed to display the widest item.
Constructor Details
#initialize(title = 'Commands') ⇒ MenuItemGroup
Returns a new instance of MenuItemGroup.
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
#items ⇒ Object (readonly)
Returns the value of attribute items.
16 17 18 |
# File 'lib/make_menu/menu_item_group.rb', line 16 def items @items end |
#title ⇒ Object (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
21 22 23 24 |
# File 'lib/make_menu/menu_item_group.rb', line 21 def add_item(item) items << item item end |
#height ⇒ Integer
Returns 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_s ⇒ String
Returns 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 |
#width ⇒ Integer
Returns 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 |