Class: Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/artsy/menu.rb

Overview

menu.rb ###

Allows you to easily create menus in your terminal programs.   ###
 Developed by Brett. (https://github.com/notronaldmcdonald)    ###

Instance Method Summary collapse

Constructor Details

#initialize(title, opts = 3, selectMode = "int") ⇒ Menu

Returns a new instance of Menu.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/artsy/menu.rb', line 9

def initialize(title, opts = 3, selectMode = "int")
  # you can set title and no. of opts for your menu object
  @menutitle = title
  @optstrings = Array.new(opts) # create an array for each option
  @optids = Array.new(opts)
  maxopts = opts - 1 # for use in the loop
  for i in 0..maxopts
    # cycle through the number of options, generating a default string for each
    @optstrings[i] = "Option #{i}"
    @optids[i] = i + 1
  end # end of loop
  @methodLoader = Linkage.new(@optids.count) # create an object, passing all necessary arguments to it
end

Instance Method Details

#assign(option, mname) ⇒ Object

end of method



48
49
50
51
52
# File 'lib/artsy/menu.rb', line 48

def assign(option, mname)
  # a wrapper function to assign a method to an option
  trueOpt = option - 1 # do some math
  @methodLoader.bindMethod(trueOpt, mname)
end

#setOpt(index, value) ⇒ Object

end of method



23
24
25
26
27
# File 'lib/artsy/menu.rb', line 23

def setOpt(index, value)
  # set the value of an option
  index_real = index - 1
  @optstrings[index_real] = "#{value}"
end

#showObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/artsy/menu.rb', line 29

def show()
  # print menu
  puts "#{@menutitle}"
  puts ""
  for i in 1..@optids.count
    # print each option
    n = i - 1 # to access strings via index
    puts "#{i}) #{@optstrings[n]}" # print the option
  end
  puts ""
  puts "Enter your selection: "
  @choice = gets.chomp
  puts "You entered #{@choice}."
  # now, we'll run some logic to ensure the choice was in @optids, then we can run the user's code!
  @choice = Integer(@choice) # make sure it is int
  @choice = @choice - 1 # convert to real
  @methodLoader.run(@choice)
end