Class: Menu::EntityAddition

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

Overview

menu class to process the addition of Persons and Tasks

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

#initializeEntityAddition

initialization



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

def initialize
  super
  @menu_description = "Person and Task Addition"
end

Instance Method Details

#add_personObject (private)

method to start the addition of a person by the PersonOption



42
43
44
45
# File 'lib/menu/entity_addition.rb', line 42

def add_person
  PersonOption.new.add_person
  nil
end

#add_taskObject (private)

method to add a new Task defined by the users console input



48
49
50
51
52
53
54
55
56
57
# File 'lib/menu/entity_addition.rb', line 48

def add_task
  t = create_task_from_input

  puts "Task with id #{t.id} created successfully.".green
  person_id =
      get_entry("Enter id of the person who should take the task: ").to_i
  Menu.data_handler.add_task_to_person(person_id, t)
  puts "Task with id #{t.id} added to person with id #{person_id}.".green
  nil
end

#create_task_from_inputTask (private)

method to create a task from the provided input

Returns:

  • (Task)

    the new task based on the input



61
62
63
64
65
66
67
68
69
# File 'lib/menu/entity_addition.rb', line 61

def create_task_from_input
  start_time = Menu.parse_date(
            get_entry("Enter start date (format: YYYY-MM-DD-hh:mm): "))
  end_time = Menu.parse_date(
            get_entry("Enter end date (format: YYYY-MM-DD-hh:mm): "))
  description = get_entry("Enter description: ")

  Task::Task.new(start_time, end_time, description)
end

#define_menu_itemsObject (private)

method to define all printable menu items



15
16
17
18
19
20
21
# File 'lib/menu/entity_addition.rb', line 15

def define_menu_items
  add_menu_item("Add person.", 1)
  add_menu_item("Add task.", 2)
  add_menu_item("Persist data and return to previous menu.", 3)
  add_menu_item("Cancel and return to previous menu.", 4)
  nil
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



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

def determine_action(input)
  case (input.to_i)
    when 1 then add_person
    when 2 then add_task
    when 3
      Menu.data_handler.persist_data
      return false
    when 4 then return false
  else
    handle_wrong_option
  end
  return true
end