Class: ConvertParallels

Inherits:
Object
  • Object
show all
Includes:
Interpipe::Interactor
Defined in:
app/interactors/convert_parallels.rb

Constant Summary collapse

DB_KEYS =
[:code, :parallel_type, :capacity, :occupied]

Instance Method Summary collapse

Methods included from Interpipe::Interactor

included, #initialize

Instance Method Details

#build_parallel(kosapi_parallel, teachers, course) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
# File 'app/interactors/convert_parallels.rb', line 26

def build_parallel(kosapi_parallel, teachers, course)
  parallel_hash = kosapi_parallel.to_hash
  parallel_hash.select! { |key,_| DB_KEYS.include? key }
  parallel_hash[:semester] = kosapi_parallel.semester.link_id
  parallel = Parallel.new(parallel_hash) { |p| p.id = kosapi_parallel.link.link_id }
  parallel.teacher_ids = teachers
  parallel.course = course
  parallel.faculty = @faculty_semester.faculty
  parallel.deleted_at = nil
  parallel
end

#extract_course(kosapi_parallel) ⇒ Object


46
47
48
49
50
# File 'app/interactors/convert_parallels.rb', line 46

def extract_course(kosapi_parallel)
  kosapi_course = kosapi_parallel.course
  course_id = kosapi_course.link_id
  @courses[course_id] ||= Course.new(name: Sequel.hstore({cs: kosapi_course.link_title})) { |c| c.id = course_id }
end

#extract_rooms(timetable_slot) ⇒ Object


58
59
60
# File 'app/interactors/convert_parallels.rb', line 58

def extract_rooms(timetable_slot)
  @rooms[timetable_slot.room.link_id] = timetable_slot.room if timetable_slot.room
end

#extract_slots(kosapi_parallel) ⇒ Object


52
53
54
55
56
# File 'app/interactors/convert_parallels.rb', line 52

def extract_slots(kosapi_parallel)
  parallel_id = kosapi_parallel.link.link_id
  kosapi_parallel.timetable_slots.each { |slot| extract_rooms(slot) }
  @timetable_slots[parallel_id] = kosapi_parallel.timetable_slots
end

#extract_teachers(kosapi_parallel) ⇒ Object


38
39
40
41
42
43
44
# File 'app/interactors/convert_parallels.rb', line 38

def extract_teachers(kosapi_parallel)
  kosapi_parallel.teachers.map do |teacher_link|
    username = teacher_link.link_id
    person = @people[username] ||= Person.new(full_name: teacher_link.link_title) { |p| p.id = username}
    person.id
  end
end

#perform(kosapi_parallels:, faculty_semester: nil) ⇒ Object


16
17
18
19
20
21
22
23
24
# File 'app/interactors/convert_parallels.rb', line 16

def perform(kosapi_parallels:, faculty_semester: nil)
  @faculty_semester = faculty_semester
  @parallels = kosapi_parallels.map do |kosapi_parallel|
    teachers = extract_teachers(kosapi_parallel)
    course = extract_course(kosapi_parallel)
    extract_slots(kosapi_parallel)
    build_parallel(kosapi_parallel, teachers, course)
  end
end

#resultsObject


62
63
64
65
66
67
68
69
70
71
# File 'app/interactors/convert_parallels.rb', line 62

def results
  {
      parallels: @parallels,
      timetable_slots: @timetable_slots,
      people: @people.values,
      courses: @courses.values,
      kosapi_rooms: @rooms.values,
      faculty_semester: @faculty_semester
  }
end

#setupObject


8
9
10
11
12
13
14
# File 'app/interactors/convert_parallels.rb', line 8

def setup
  @parallels = []
  @timetable_slots = {}
  @people = {}
  @courses = {}
  @rooms = {}
end