Class: Menu::MainMenu
Overview
This class creates the main menu for the script and holds methods to load existing data or to create a new database. After that it delegates the queries and additions to the data to the responsible classes
Instance Attribute Summary
Attributes inherited from Base
#menu_description, #menu_items
Instance Method Summary collapse
-
#create_database ⇒ Object
private
method to start the creation of the new database.
-
#define_menu_items ⇒ Object
private
method to define all printable menu items.
-
#determine_action(input) ⇒ Boolean
private
method to process the provided input.
-
#finish_database_initialization(filename) ⇒ Object
private
method to finalize the database initialization and the call of the next menu.
-
#get_database_name(message) ⇒ String
private
method to display the provided message and read the filename of the database.
-
#initialize ⇒ MainMenu
constructor
initialization.
-
#load_database ⇒ Object
private
method to load an existing database.
-
#print_error(message) ⇒ Object
private
method to print an error message.
Methods inherited from Base
#add_menu_item, #get_entry, #handle_wrong_option, #print_menu
Constructor Details
#initialize ⇒ MainMenu
initialization
9 10 11 12 |
# File 'lib/menu/main_menu.rb', line 9 def initialize super @menu_description = "Work Accounting v0.3.1. What do you want to do?" end |
Instance Method Details
#create_database ⇒ Object (private)
method to start the creation of the new database. The user is asked to provide a name for the database or file
46 47 48 49 50 |
# File 'lib/menu/main_menu.rb', line 46 def create_database filename = get_database_name("Create a new database.") AdapterMenu.new(filename). finish_database_initialization(filename) end |
#define_menu_items ⇒ Object (private)
method to define all printable menu items
17 18 19 20 21 |
# File 'lib/menu/main_menu.rb', line 17 def ("Create a new database.", 1) ("Load an existing database.", 2) ("Exit.", 3) end |
#determine_action(input) ⇒ Boolean (private)
method to process the provided input
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/menu/main_menu.rb', line 33 def determine_action(input) case (input.to_i) when 1 then create_database when 2 then load_database when 3 then Menu.exit_script else handle_wrong_option end return true end |
#finish_database_initialization(filename) ⇒ Object (private)
method to finalize the database initialization and the call of the next menu
68 69 70 71 |
# File 'lib/menu/main_menu.rb', line 68 def finish_database_initialization(filename) puts "Database created from: #{filename}.".green DatabaseOption.new. end |
#get_database_name(message) ⇒ String (private)
method to display the provided message and read the filename of the database
77 78 79 80 |
# File 'lib/menu/main_menu.rb', line 77 def get_database_name() puts get_entry("Input a name for the database: ") end |
#load_database ⇒ Object (private)
method to load an existing database. The user is asked to provide the path to the database
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/menu/main_menu.rb', line 54 def load_database filename = get_database_name("Load an existing database.") begin AdapterMenu.new(filename). rescue IOError => e puts e..red return end finish_database_initialization(filename) end |
#print_error(message) ⇒ Object (private)
method to print an error message
25 26 27 |
# File 'lib/menu/main_menu.rb', line 25 def print_error() puts .red end |