Class: TeacherTimetableSlotTransformer

Inherits:
Object
  • Object
show all
Includes:
Celluloid, ETLConsumer, ETLProducer
Defined in:
lib/actors/teacher_timetable_slot_transformer.rb

Overview

A convertor which receives TeacherTimetableSlots loaded from KOSapi and plans them into Events according to semester parameters and semester periods.

Instance Method Summary collapse

Methods included from ETLConsumer

#consume_row, #input=, #notify_hungry, #pop_processed_row, #processed_row, #processed_row=, #start!

Methods included from ETLBase

#empty?, #eof_received?, #logger, #mark_empty!, #receive_eof, #unmark_empty!

Methods included from ETLProducer

#buffer_empty?, #buffer_full?, #buffer_pop, #buffer_put, #emit_eof, #emit_row, #output=, #output_hungry?, #output_row, #process_eof, #produce_row, #receive_hungry

Constructor Details

#initialize(input, output, semester) ⇒ TeacherTimetableSlotTransformer

Returns a new instance of TeacherTimetableSlotTransformer.


15
16
17
18
19
20
# File 'lib/actors/teacher_timetable_slot_transformer.rb', line 15

def initialize(input, output, semester)
  self.input = input
  self.output = output
  @semester = semester
  @events = nil
end

Instance Method Details

#generate_rowArray<Event>

Returns generated events that were not yet synced with the database.

Returns:

  • (Array<Event>)

    generated events that were not yet synced with the database


31
32
33
34
35
36
37
# File 'lib/actors/teacher_timetable_slot_transformer.rb', line 31

def generate_row
  if processed_row
    pop_processed_row
  else
    raise EndOfData
  end
end

#plan_events(slot, teacher) ⇒ Array<Event>

Returns planned events.

Parameters:

  • slot (KOSapiClient::Entity::TeacherTimetableSlot)

    teacher timetable slot loaded from KOSapi

  • teacher (String)

    teacher username for the timetable slot

Returns:

  • (Array<Event>)

    planned events


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/actors/teacher_timetable_slot_transformer.rb', line 42

def plan_events(slot, teacher)
  periods_query = @semester.semester_periods_dataset
    .where(type: ['teaching', 'exams'])
    .order(:starts_at)
  periods = periods_query.map { |p| PlannedSemesterPeriod.new(p) }
  time_converter = Sirius::TimeConverter.new(
    hour_starts: @semester.hour_starts,
    hour_length: @semester.hour_duration
  )  # in case the duration is not set, use default duration of 2 hours

  unless slot.duration
    slot.duration = 2
  end
  slot.parity = slot.parity.to_s
  events = periods.flat_map do |period|
    PlannedTimetableSlot.new(slot, time_converter).generate_events(@semester, period)
  end

  events.each_with_index do |e, i|
    e.event_type = 'teacher_timetable_slot'
    e.source_type = 'teacher_timetable_slot'
    e.source_id = slot.id
    e.teacher_ids = [teacher]
    e.student_ids = []
    e.absolute_sequence_number = i + 1
    e.name = Sequel.hstore({cs: slot.title})
  end

  events
end

#process_row(row) ⇒ Array<Event>

Returns planned events.

Parameters:

  • row (Array(KOSapiClient::Entity::TeacherTimetableSlot, String))

    teacher timetable slot together with related teacher username

Returns:

  • (Array<Event>)

    planned events


25
26
27
28
# File 'lib/actors/teacher_timetable_slot_transformer.rb', line 25

def process_row(row)
  slot, teacher = *row
  plan_events(slot, teacher)
end