Class: Menu::MainMenu

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

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

Methods inherited from Base

#add_menu_item, #get_entry, #handle_wrong_option, #print_menu

Constructor Details

#initializeMainMenu

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_databaseObject (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).print_menu
  finish_database_initialization(filename)
end

#define_menu_itemsObject (private)

method to define all printable menu items



17
18
19
20
21
# File 'lib/menu/main_menu.rb', line 17

def define_menu_items
  add_menu_item("Create a new database.", 1)
  add_menu_item("Load an existing database.", 2)
  add_menu_item("Exit.", 3)
end

#determine_action(input) ⇒ Boolean (private)

method to process the provided input

Parameters:

  • input (String)

    the provided input

Returns:

  • (Boolean)

    true: if the program should continue, false: if the script should exit



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

Parameters:

  • filename (String)

    the provided filename



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.database_menu
end

#get_database_name(message) ⇒ String (private)

method to display the provided message and read the filename of the database

Parameters:

  • message (String)

    the output message

Returns:

  • (String)

    the provided filename



77
78
79
80
# File 'lib/menu/main_menu.rb', line 77

def get_database_name(message)
  puts message
  get_entry("Input a name for the database: ")
end

#load_databaseObject (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).print_menu
  rescue IOError => e
    puts e.message.red
    return
  end
  finish_database_initialization(filename)
end

method to print an error message

Parameters:

  • message (String)

    the error message that should be printed



25
26
27
# File 'lib/menu/main_menu.rb', line 25

def print_error(message)
  puts message.red
end