Class: Menu::PersonOption

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

Overview

menu class to process the add option for persons

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

#initializePersonOption

initialization



7
8
9
10
# File 'lib/menu/person_option.rb', line 7

def initialize
  super
  @menu_description = "Adds a new Person. Select Type: "
end

Instance Method Details

#add_personObject

public entry point to print the menu and execute to chosen option



13
14
15
# File 'lib/menu/person_option.rb', line 13

def add_person
  print_menu
end

#add_simple_personObject (private)

method to create and add a simple person



42
43
44
45
46
47
48
# File 'lib/menu/person_option.rb', line 42

def add_simple_person
  name = get_entry("Enter name: ")

  p = Person::Person.new(name)
  Menu.data_handler.add_person(p)
  puts "Person with id #{p.id} added successfully.".green
end

#add_studentObject (private)

method to create and add a student



51
52
53
54
55
56
57
58
# File 'lib/menu/person_option.rb', line 51

def add_student
  name = get_entry("Enter name: ")
  mat_nr = get_entry("Enter matriculation number: ").to_i

  s = Person::Student.new(name, mat_nr)
  Menu.data_handler.add_person(s)
  puts "Student with id #{s.id} added successfully.".green
end

#define_menu_itemsObject (private)

method to define all printable menu items



20
21
22
23
24
# File 'lib/menu/person_option.rb', line 20

def define_menu_items
  add_menu_item("Person.", 1)
  add_menu_item("Student.", 2)
  add_menu_item("Cancel and return to previous menu.", 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 return to the previous menu



30
31
32
33
34
35
36
37
38
39
# File 'lib/menu/person_option.rb', line 30

def determine_action(input)
  case (input.to_i)
    when 1 then add_simple_person
    when 2 then add_student
    when 3 then return false
  else
    handle_wrong_option
  end
  return true
end