Class: Menu::TimeMenu::TimeMenu

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

Overview

menu class that serves as a base class for menus used to query information for a given time interval

Direct Known Subclasses

CustomtimeMenu, IntervaltimeMenu

Instance Attribute Summary collapse

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

#initialize(time_string) ⇒ TimeMenu

initialization



12
13
14
15
16
17
# File 'lib/menu/worktime/time_menu.rb', line 12

def initialize(time_string)
  @time_string = time_string
  @menu_description = "Queries for tasks done in the given " \
                      "#{@time_string}."
  super
end

Instance Attribute Details

#additionsArray (readonly, private)

Returns an array with specific additions for the output.

Returns:

  • (Array)

    an array with specific additions for the output



26
27
28
# File 'lib/menu/worktime/time_menu.rb', line 26

def additions
  @additions
end

#time_stringString (readonly, private)

Returns the string describing the given time interval.

Returns:

  • (String)

    the string describing the given time interval



21
22
23
# File 'lib/menu/worktime/time_menu.rb', line 21

def time_string
  @time_string
end

#valuesHash (readonly, private)

them as long as this menu is active

Returns:

  • (Hash)

    a hash containing the requested input values to reuse



24
25
26
# File 'lib/menu/worktime/time_menu.rb', line 24

def values
  @values
end

Instance Method Details

#calculate_worktimeString (private)

method to calculate the overall worktime an return it

Returns:

  • (String)

    a formatted string containing the calculated worktime



170
171
172
173
174
175
176
177
178
# File 'lib/menu/worktime/time_menu.rb', line 170

def calculate_worktime
  times = retrieve_worktime
  if (times[:over] > 0)
    time = times[:over]
  else
    time = times[:during] + times[:into] + times[:beyond]
  end
  "Worktime:; #{time} h"
end

#check_attributesObject (private)

method to check if the necessary input was already collected



112
113
114
115
116
117
118
119
# File 'lib/menu/worktime/time_menu.rb', line 112

def check_attributes
  if (@values == nil)
    puts "Specify necessary informations: "
    get_input_values
    @additions = Array.new()
  end
  #check_time_string
end

#check_time_stringObject (private)

method check, if the subclass has set a value for the class attribute

Raises:

  • (NotImplementedError)

    if the subclass does not have set the value



123
124
125
126
127
128
129
# File 'lib/menu/worktime/time_menu.rb', line 123

def check_time_string
  if @time_string.eql?(nil)
    fail NotImplementedError, " Error: the subclass " \
    "#{self.name.split("::").last} does not present a string for " \
    "its attribute time_string".red
  end
end

#define_menu_itemsObject (private)

method to define all printable menu items



39
40
41
42
43
44
45
46
47
# File 'lib/menu/worktime/time_menu.rb', line 39

def define_menu_items
  check_attributes
  @menu_description.concat("\n #{set_boundaries}")
  add_menu_item("All tasks to a person in the given #{time_string}.", 1)
  add_menu_item("Worktime of a person in that #{time_string}.", 2)
  add_menu_item("All tasks running during the interval.", 3)
  add_menu_item("Write Query result to csv-file.", 4)
  add_menu_item("Cancel and return to previous menu.", 5)
end

#determine_action(input) ⇒ Boolean (private)

method to process the provided input

Parameters:

  • input (Integer)

    the provided input

Returns:

  • (Boolean)

    true: if the a query type was used, false: if the script should return to the previous menu



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/menu/worktime/time_menu.rb', line 53

def determine_action(input)
  case (input.to_i)
    when 1 then print_all_tasks
    when 2 then retrieve_and_print_worktime
    when 3 then print_tasks_in_interval
    when 4 then output_to_csv
    when 5
      @values = nil
      return false
  else
    handle_wrong_option
  end
  return true
end

#get_input_values(time_string) ⇒ Object (private)

This method is abstract.

subclasses need to implement this method

abstract method to retrieve the required input values

Parameters:

  • time_string (String)

    the string for the prompt collecting the time value



32
33
34
35
36
# File 'lib/menu/worktime/time_menu.rb', line 32

def get_input_values(time_string)
  fail NotImplementedError, " Error: the subclass " \
  "#{self.name.split("::").last} needs to implement the method: " \
  "get_input_values from its parent class".red
end

#output_to_csvObject (private)

method to write the results into a csv formatted file



157
158
159
160
161
162
163
164
165
166
# File 'lib/menu/worktime/time_menu.rb', line 157

def output_to_csv
  if (@values[:result] != nil)
    @additions << calculate_worktime
    filename = get_entry("Specify output file: ")
    p = Menu.data_handler.find_person_by_id(@values[:id])
    CSVWriter.output(filename, p, @values[:result], @additions)
  else
    puts "Nothing to write right now."
  end
end

method to print all the collected tasks



132
133
134
135
136
137
138
# File 'lib/menu/worktime/time_menu.rb', line 132

def print_all_tasks
  tasks = retrieve_tasks
  @values[:result] = Array.new()
  tasks.each_key { |key|
    print_tasks_to_key(tasks[key]) if (tasks[key].size > 0)
  }
end

method to print all tasks in the given time interval



150
151
152
153
154
# File 'lib/menu/worktime/time_menu.rb', line 150

def print_tasks_in_interval
  tasks = retrieve_tasks
  @values[:result] = Array.new()
  print_tasks_to_key(tasks[:during])
end

method to print all tasks collected to a specific key

Parameters:

  • tasks (Array)

    an array containing all tasks of a given key



142
143
144
145
146
147
# File 'lib/menu/worktime/time_menu.rb', line 142

def print_tasks_to_key(tasks)
  tasks.each { |task|
    puts task.to_string
    @values[:result] << task
  }
end

#retrieve_and_print_worktimeObject (private)

method to print the worktime hours of the retrieved tasks



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/menu/worktime/time_menu.rb', line 69

def retrieve_and_print_worktime
  times = retrieve_worktime

  puts "printing new worktime"
  if (times[:over] > 0)
    puts "Worktime: #{times[:over]} h"
  else
    puts "tasks.over: 0.0 h"
    puts "tasks.during: #{times[:during]} h"
    puts "tasks.into: #{times[:into]} h"
    puts "tasks.beyond: #{times[:beyond]} h"
  end
end

#retrieve_tasksObject (private)

This method is abstract.

subclasses need to implement this method

abstract method to retrieve the tasks for the given interval

Raises:

  • (NotImplementedError)

    if the subclass does not have this method



86
87
88
89
90
# File 'lib/menu/worktime/time_menu.rb', line 86

def retrieve_tasks
  fail NotImplementedError, " Error: the subclass " \
  "#{self.name.split("::").last} needs to implement the method: " \
  "retrieve_tasks from its parent class".red
end

#retrieve_worktimeObject (private)

This method is abstract.

subclasses need to implement this method

abstract method to retrieve the worktime for the given interval

Raises:

  • (NotImplementedError)

    if the subclass does not have this method



95
96
97
98
99
# File 'lib/menu/worktime/time_menu.rb', line 95

def retrieve_worktime
  fail NotImplementedError, " Error: the subclass " \
  "#{self.name.split("::").last} needs to implement the method: " \
  "retrieve_worktime from its parent class".red
end

#set_boundariesObject (private)

This method is abstract.

subclasses need to implement this method

abstract method to calculate the date boundaries of the provided user input

Raises:

  • (NotImplementedError)

    if the subclass does not have this method



105
106
107
108
109
# File 'lib/menu/worktime/time_menu.rb', line 105

def set_boundaries
  fail NotImplementedError, " Error: the subclass " \
  "#{Class.name.split("::").last} needs to implement the method: " \
  "set_boundaries from its base class".red
end