Class: MakeMenu::MenuItem

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

Overview

This class represents an option in the menu which runs a target from the Makefile

Constant Summary collapse

INDENT =
6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option_number = nil, target = nil, description = nil) ⇒ MenuItem

Returns a new instance of MenuItem.



8
9
10
11
12
# File 'lib/make_menu/menu_item.rb', line 8

def initialize(option_number = nil, target = nil, description = nil)
  @option_number = option_number
  @target = target
  @description = description || target
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



14
15
16
# File 'lib/make_menu/menu_item.rb', line 14

def description
  @description
end

#option_numberObject (readonly)

Returns the value of attribute option_number.



14
15
16
# File 'lib/make_menu/menu_item.rb', line 14

def option_number
  @option_number
end

#targetObject (readonly)

Returns the value of attribute target.



14
15
16
# File 'lib/make_menu/menu_item.rb', line 14

def target
  @target
end

Instance Method Details

#executeObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/make_menu/menu_item.rb', line 16

def execute
  cmd = ['make', target]
  puts "> #{cmd.join(' ').cyan}\n"
  unless system(*cmd)
    # Indicates the command failed, so we pause to allow user to see error message
    puts "\nPress ENTER to continue....".dark
    gets
  end
rescue StandardError => _e
  # Sink keyboard interrupt from within Make target
end

#to_sString

Returns Text to display for this item.

Returns:

  • (String)

    Text to display for this item



34
35
36
# File 'lib/make_menu/menu_item.rb', line 34

def to_s
  "#{option_number.to_s.rjust(INDENT, ' ').bold}.  #{description}"
end

#widthInteger

Returns Number of characters required to display the item.

Returns:

  • (Integer)

    Number of characters required to display the item



29
30
31
# File 'lib/make_menu/menu_item.rb', line 29

def width
  description.size + INDENT + 1
end