-
Notifications
You must be signed in to change notification settings - Fork 2
/
menu.rb
48 lines (40 loc) · 994 Bytes
/
menu.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Menu
MENU_OPTIONS = {
'1' => :create_music_album,
'2' => :create_book,
'3' => :create_game,
'4' => :list_books,
'5' => :list_games,
'6' => :list_music_albums,
'7' => :list_genres,
'8' => :list_authors,
'9' => :list_labels,
'10' => :exit
}.freeze
def initialize(app)
@app = app
end
def show
loop do
print_menu_options
choice = menu_choice
if MENU_OPTIONS.include?(choice) # If the key is present
method_name = MENU_OPTIONS[choice] # get value
exit if method_name == :exit
@app.send(method_name)
else
puts 'Invalid input. Please try again.'
end
end
end
private
def print_menu_options
puts "\nPlease choose an option by entering a number:"
MENU_OPTIONS.each do |key, value|
puts "#{key} - #{value.to_s.gsub('_', ' ')}" # replace underscores with spaces in the value string
end
end
def menu_choice
gets.chomp.downcase
end
end