Class: Menu::EntityQueries

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

Overview

menu class to process the queries 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

#initializeEntityQueries

initialization



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

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

Instance Method Details

#define_menu_itemsObject (private)

method to define all printable menu items



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

def define_menu_items
  add_menu_item("Query person.", 1)
  add_menu_item("Query task.", 2)
  add_menu_item("Query tasks to person.", 3)
  add_menu_item("Query persons.", 4)
  add_menu_item("Query tasks.", 5)
  add_menu_item("Cancel and return to previous menu.", 6)
  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



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

def determine_action(input)
  case (input.to_i)
    when 1 then query_person
    when 2 then query_task
    when 3 then query_tasks_to_person
    when 4 then query_all_persons
    when 5 then query_all_tasks
    when 6 then return false
  else
    handle_wrong_option
  end
  return true
end

#output_results(results) ⇒ Object (private)

method to print a given array of result entities

Parameters:

  • results (Array)

    the results of a given query



92
93
94
95
96
97
# File 'lib/menu/entity_queries.rb', line 92

def output_results(results)
  results.each { |result|
    puts result.to_string
  }
  nil
end

#query_all_personsObject (private)

method to query all persons of the database



57
58
59
60
# File 'lib/menu/entity_queries.rb', line 57

def query_all_persons
  output_results(Menu.data_handler.get_persons)
  nil
end

#query_all_tasksObject (private)

method to query all tasks of the database



63
64
65
66
# File 'lib/menu/entity_queries.rb', line 63

def query_all_tasks
  output_results(Menu.data_handler.get_tasks)
  nil
end

#query_personObject (private)

method to query a person from the database by id

Raises:

  • (NoMethodError)

    if no person can be found by the given id



45
46
47
48
49
50
51
52
53
54
# File 'lib/menu/entity_queries.rb', line 45

def query_person
  begin
    id = get_entry("Enter id: ").to_i
    p = Menu.data_handler.find_person_by_id(id)
    puts "Result: #{p.to_string}\n\n"
  rescue NoMethodError
    puts "Could not found person with id #{id}.".red
  end
  nil
end

#query_taskObject (private)

method to query a task from the database by its id



69
70
71
72
73
74
# File 'lib/menu/entity_queries.rb', line 69

def query_task
  id = get_entry("Enter id: ").to_i
  task = Menu.data_handler.find_task_to_id(id)
  puts task.to_string
  nil
end

#query_tasks_to_personObject (private)

method to query all tasks belonging to a specified person



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/menu/entity_queries.rb', line 77

def query_tasks_to_person
  begin
    id = get_entry("Enter id: ").to_i
    t = Menu.data_handler.get_tasks_to_person(id)
    puts "#{t.size} tasks found".yellow
    output_results(t)
    puts
  rescue NoMethodError
    puts "Could not found person with id #{id}.".red
  end
  nil
end