Module: Query

Defined in:
lib/query/query.rb,
lib/query/time_accumulator.rb

Overview

This module holds the classes and methods for queries regarding the data

Defined Under Namespace

Classes: TimeAccumulator

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dataBaseHandler (readonly)

Returns the database with the Tasks and Persons.

Returns:

  • (BaseHandler)

    the database with the Tasks and Persons



9
10
11
# File 'lib/query/query.rb', line 9

def data
  @data
end

Class Method Details

.collect_tasks_beyond(start_time, end_time, all_task) ⇒ Hash

method to collect all task that start in the time interval but do not end in the requested interval

Parameters:

  • start_time (Time)

    the start time of the time interval

  • end_time (Time)

    the end time of the time interval

  • all_task (Array)

    the task with start and/ or end within the requested time interval

Returns:

  • (Hash)

    all tasks starting in the given time frame



124
125
126
127
128
129
# File 'lib/query/query.rb', line 124

def self.collect_tasks_beyond(start_time, end_time, all_task)
  all_task.select { |task|
    time_lies_in_interval?(task.start_time, start_time, end_time) &&
    task_ends_after?(task, end_time)
  }
end

.collect_tasks_during(start_time, end_time, all_task) ⇒ Hash

method to collect all task that occur during the time interval

Parameters:

  • start_time (Time)

    the start time of the time interval

  • end_time (Time)

    the end time of the time interval

  • all_task (Array)

    the task with start and/ or end within the requested time interval

Returns:

  • (Hash)

    all tasks taking place during the given time frame



79
80
81
82
83
84
# File 'lib/query/query.rb', line 79

def self.collect_tasks_during(start_time, end_time, all_task)
  all_task.select { |task|
    !task_starts_before?(task, start_time) &&
    !task_ends_after?(task, end_time)
  }
end

.collect_tasks_into(start_time, end_time, all_task) ⇒ Hash

method to collect all task that end in the time interval but were started before the requested interval

Parameters:

  • start_time (Time)

    the start time of the time interval

  • end_time (Time)

    the end time of the time interval

  • all_task (Array)

    the task with start and/ or end within the requested time interval

Returns:

  • (Hash)

    all tasks ending in the given time frame



109
110
111
112
113
114
# File 'lib/query/query.rb', line 109

def self.collect_tasks_into(start_time, end_time, all_task)
  all_task.select { |task|
    task_starts_before?(task, start_time) &&
    time_lies_in_interval?(task.end_time, start_time, end_time)
  }
end

.collect_tasks_over(start_time, end_time, all_task) ⇒ Hash

method to collect all task that start before the time interval but do not end in the time interval

Parameters:

  • start_time (Time)

    the start time of the time interval

  • end_time (Time)

    the end time of the time interval

  • all_task (Array)

    the task with start and/ or end within the requested time interval

Returns:

  • (Hash)

    all tasks running over the given time frame



94
95
96
97
98
99
# File 'lib/query/query.rb', line 94

def self.collect_tasks_over(start_time, end_time, all_task)
  all_task.select { |task|
    task_starts_before?(task, start_time) &&
    task_ends_after?(task, end_time)
  }
end

.get_data(id, start_time, end_time) ⇒ Hash

method to collect all tasks of a person in a given time interval

Parameters:

  • id (Integer)

    the id of the person

  • start_time (Time)

    the start time of the time interval

  • end_time (Time)

    the end time of the time interval

Returns:

  • (Hash)

    a hash with all tasks mapped on how the occur in the time interval



18
19
20
21
22
23
24
25
26
27
# File 'lib/query/query.rb', line 18

def self.get_data(id, start_time, end_time)
  all_task = Query.data.get_tasks_to_person(id)
  tasks = {
    :during => collect_tasks_during(start_time, end_time, all_task),
    :over => collect_tasks_over(start_time, end_time, all_task),
    :into => collect_tasks_into(start_time, end_time, all_task),
    :beyond => collect_tasks_beyond(start_time, end_time, all_task)
  }
  return tasks
end

.get_monthly_worktime(id, start_time, end_time) ⇒ Hash

method to retrieve the worktime for a person for the given month

Parameters:

  • id (Integer)

    the id of the person

  • start_time (Time)

    the start time of the time interval

  • end_time (Time)

    the end time of the time interval

Returns:

  • (Hash)

    a hash containing all time values for the considered intervals



46
47
48
49
50
51
# File 'lib/query/query.rb', line 46

def self.get_monthly_worktime(id, start_time, end_time)
  days_in_month = Time.days_in_month(start_time.year,start_time.month)
  tasks = get_data(id, start_time, end_time)
  TimeAccumulator.get_interval_worktime(tasks, start_time, end_time,
                                        (days_in_month * 24))
end

.get_time_worktime(id, start_time, end_time) ⇒ Hash

method to retrieve the worktime for a person for the given time interval

Parameters:

  • id (Integer)

    the id of the person

  • start_time (Time)

    the start time of the time interval

  • end_time (Time)

    the end time of the time interval

Returns:

  • (Hash)

    a hash containing all time values for the considered intervals



60
61
62
63
64
# File 'lib/query/query.rb', line 60

def self.get_time_worktime(id, start_time, end_time)
  tasks = get_data(id, start_time, end_time)
  TimeAccumulator.get_interval_worktime(tasks, start_time, end_time,
                                       (end_time - start_time) / 60 / 60)
end

.get_weekly_worktime(id, start_time, end_time) ⇒ Hash

method to retrieve the worktime for a person for the given week

Parameters:

  • id (Integer)

    the id of the person

  • start_time (Time)

    the start time of the time interval

  • end_time (Time)

    the end time of the time interval

Returns:

  • (Hash)

    a hash containing all time values for the considered intervals



35
36
37
38
# File 'lib/query/query.rb', line 35

def self.get_weekly_worktime(id, start_time, end_time)
  tasks = get_data(id, start_time, end_time)
  TimeAccumulator.get_interval_worktime(tasks, start_time, end_time, (7*24))
end

.initialize_repository(data) ⇒ Object

singleton method to initialize the data repository with the provided data

Parameters:



68
69
70
# File 'lib/query/query.rb', line 68

def self.initialize_repository(data)
  @data = data
end

.task_ends_after?(task, date_value) ⇒ Boolean

method to check if a given task ends after the specified date

Parameters:

  • task (Task)

    the given task

  • date_value (Time)

    the considered point in time

Returns:

  • (Boolean)

    true: if the task ends after the given point in time false: if not



158
159
160
# File 'lib/query/query.rb', line 158

def self.task_ends_after?(task, date_value)
  task.end_time > date_value
end

.task_starts_before?(task, date_value) ⇒ Boolean

method to check if a given task starts before the specified date

Parameters:

  • task (Task)

    the given task

  • date_value (Time)

    the considered point in time

Returns:

  • (Boolean)

    true: if the task starts before the given point in time false: if not



148
149
150
# File 'lib/query/query.rb', line 148

def self.task_starts_before?(task, date_value)
  task.start_time < date_value
end

.time_lies_in_interval?(time, start_time, end_time) ⇒ Boolean

method to determine if the point in time lies within the time interval given by the data values

Parameters:

  • time (Time)

    the given point in time

  • start_time (Time)

    the start time of the time interval

  • end_time (Time)

    the end time of the time interval

Returns:

  • (Boolean)

    true: if time lies within the interval, false: if not



138
139
140
# File 'lib/query/query.rb', line 138

def self.time_lies_in_interval?(time, start_time, end_time)
  time > start_time && time < end_time
end