Class: Syctask::Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/syctask/schedule.rb

Overview

Schedule represents a working day with a start and end time, meeting times and titles and tasks. Tasks can also be associated to meetings as in an agenda. Invokation example

work = ["8","30","18","45"]
busy = [["9","0","10","0"],["11","30","12","15"]]
titles = ["Ruby class room training","Discuss Ruby"]
tasks = [task1,task2,task3,task4,task5,task6]
schedule = Syctask::Schedule.new(work,busy,titles,tasks)
schedule.graph.each {|output| puts output}

This will create following output

Meetings
--------
A - Ruby class room training
B - Discuss Ruby

    A         B
xxoo/////xxx|-////oooooxoooo|---|---|---|---|
8   9  10  11  12  13  14  15  16  17  18  19
1 2      3        4    5
                        6

Tasks
-----
0 - 1: task1
1 - 2: task2
2 - 3: task3
3 - 4: task4
4 - 5: task5
5 - 6: task6

Subsequent tasks are are displayed in the graph alternating with x and o. Meetings are indicated with / and the start is marked with A, B and so on. Task IDs are shown below the graph. The graph will be printed colored. Meetings in red, free times in green and tasks in blue. The past time is shown in black.

Constant Summary collapse

BUSY_COLOR =

Color of meetings

:red
FREE_COLOR =

Color of free times

:green
WORK_COLOR =

Color of tasks

:blue
UNSCHEDULED_COLOR =

If tasks cannot be assigned to the working time this color is used

:yellow
GRAPH_PATTERN =

Regex scans tasks and free times in the graph

%r{[|-]+|/+|[xo]+}
BUSY_PATTERN =

Regex scans meetings in the graph

%r{/+}
FREE_PATTERN =

Regex scans free times in the graph

/[|-]+/
WORK_PATTERN =

Regex scans tasks in the graph

/[xo]+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(work_time, busy_time = [], titles = [], tasks = []) ⇒ Schedule

Creates a new Schedule and initializes work time, busy times, titles and tasks. Work time is mandatory, busy times, titles and tasks are optional. Values have to be provided as

  • work time: [start_hour, start_minute, end_hour, end_minute]

  • busy time: [[start_hour, start_minute, end_hour, end_minute],]

  • titles: [title,…]

  • tasks: [task,…]



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/syctask/schedule.rb', line 81

def initialize(work_time, busy_time = [], titles = [], tasks = [])
  @starts = Syctask::Times.new([work_time[0], work_time[1]])
  @ends = Syctask::Times.new([work_time[2], work_time[3]])
  @meetings = []
  titles ||= []
  busy_time.each.with_index do |busy, index|
    title = titles[index] || "Meeting #{index}"
    @meetings << Syctask::Meeting.new(busy, title)
  end
  unless within?(@meetings,
                 @starts,
                 @ends)
    raise Exception,
          'Busy times have to be within work time'
  end
  @tasks = tasks
end

Instance Attribute Details

#endsObject (readonly)

End time of working day



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

def ends
  @ends
end

#meetingsObject

Meetings assigned to the work time



70
71
72
# File 'lib/syctask/schedule.rb', line 70

def meetings
  @meetings
end

#startsObject (readonly)

Start time of working day



66
67
68
# File 'lib/syctask/schedule.rb', line 66

def starts
  @starts
end

#tasksObject

Tasks assigned to the work time



72
73
74
# File 'lib/syctask/schedule.rb', line 72

def tasks
  @tasks
end

Instance Method Details

#assign(assignments) ⇒ Object

Sets the assignments containing tasks that are assigned to meetings. Returns true if succeeds



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/syctask/schedule.rb', line 101

def assign(assignments)
  assignments.each do |assignment|
    number = assignment[0].upcase.ord - 'A'.ord
    return false if number.negative? || number > @meetings.size

    @meetings[number].tasks.clear
    assignment[1].split(',').each do |id|
      index = @tasks.find_index { |task| task.id == id.to_i }
      @meetings[number].tasks << @tasks[index] if index && @tasks[index]
    end
    @meetings[number].tasks.uniq!
  end
  true
end

#get_timesObject

Retrieves the work and busy times transformed to the time line scale



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/syctask/schedule.rb', line 403

def get_times
  work_time = [@starts.h, @ends.round_up]
  meeting_times = []
  if @meetings
    @meetings.each do |meeting|
      meeting_time = Array.new(2)
      meeting_time[0] = hour_offset(@starts.h, meeting.starts.h) +
                        minute_offset(meeting.starts.m)
      meeting_time[1] = hour_offset(@starts.h, meeting.ends.h) +
                        minute_offset(meeting.ends.m)
      meeting_times << meeting_time
    end
  end

  [work_time, meeting_times]
end

#graphObject

graph first creates creates the time line. Then the busy times are added. After that the tasks are added to the time line and the task caption and task list is created. graph returns the graph, task caption, task list and meeting list

  • time line

  • add meetings to time line

  • add tasks to time line

  • create task caption

  • create task list

  • create meeting caption

  • create meeting list

  • return time line, task caption, task list, meeting caption and meeting

list



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/syctask/schedule.rb', line 173

def graph
  (work_time, meeting_times) = get_times

  heading = format('+++ %s - %s-%s +++', Time.now.strftime('%Y-%m-%d'),
                   @starts.time.strftime('%H:%M'),
                   @ends.time.strftime('%H:%M')).color(:blue)

  time_line = '|---' * (work_time[1] - work_time[0]) + '|'
  meeting_times.each do |time|
    time_line[time[0]..time[1] - 1] = '/' * (time[1] - time[0])
  end

  task_list, task_caption = assign_tasks_to_graph(time_line)

  [heading.center(80), meeting_list, meeting_caption,
   colorize(time_line), time_caption,
   task_caption, task_list]
end

#meeting_captionObject

Creates a meeting caption and returns it for printing



139
140
141
142
143
144
145
146
147
148
# File 'lib/syctask/schedule.rb', line 139

def meeting_caption
  (_, meeting_times) = get_times
  caption = ''
  meeting_number = 'A'
  meeting_times.each do |times|
    caption << ' ' * (times[0] - caption.size) + meeting_number
    meeting_number.next!
  end
  format('%s', caption).color(:red)
end

#meeting_listObject

Creates a meeting list for printing. Returns the meeting list



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/syctask/schedule.rb', line 117

def meeting_list
  list = format('%s', "Meetings\n").color(:red)
  list << format('%s', "--------\n").color(:red)
  meeting_number = 'A'
  @meetings.each do |meeting|
    hint = '-'
    hint = '*' if time_between?(Time.now,
                                meeting.starts.time,
                                meeting.ends.time)
    list << format("%s %s %s\n", meeting_number,
                   hint,
                   meeting.title).color(:red)
    meeting_number.next!
    meeting.tasks.each do |task|
      task_color = task.done? ? :green : :blue
      list << format("%5s - %s\n", task.id, task.title).color(task_color)
    end
  end
  list
end

#time_captionObject

Creates the time caption for the time line



151
152
153
154
155
156
157
158
# File 'lib/syctask/schedule.rb', line 151

def time_caption
  work_time = get_times[0]
  caption = ''
  work_time[0].upto(work_time[1]) do |time|
    caption << time.to_s + (time < 9 ? ' ' * 3 : ' ' * 2)
  end
  format('%s', caption)
end