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.

Parameters:

  • option_number (Integer) (defaults to: nil)

    Number user enters for this command

  • target (String) (defaults to: nil)

    Name of target defined in Makefile

  • description (String) (defaults to: nil)

    Text to display for this command, taken from Makefile comment



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

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.



17
18
19
# File 'lib/make_menu/menu_item.rb', line 17

def description
  @description
end

#option_numberObject (readonly)

Returns the value of attribute option_number.



17
18
19
# File 'lib/make_menu/menu_item.rb', line 17

def option_number
  @option_number
end

#targetObject (readonly)

Returns the value of attribute target.



17
18
19
# File 'lib/make_menu/menu_item.rb', line 17

def target
  @target
end

Instance Method Details

#executeObject

Run the make target



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/make_menu/menu_item.rb', line 20

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 key to continue....\n"
    gets
  end
rescue StandardError => _e
  # ignore CTRL+C from within Make target
end

#to_sString

Returns Text to display for this item.

Returns:

  • (String)

    Text to display for this item



38
39
40
# File 'lib/make_menu/menu_item.rb', line 38

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



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

def width
  description.size + INDENT + 1
end