class Generator::GroupsOfNStudents

Attributes

final_groups[RW]
groups_of[RW]
number_of_groups[RW]
sort_type[RW]
students[RW]

Public Class Methods

new(students:, groups_of:, sort_type:) click to toggle source
# File lib/learn_together/generator.rb, line 32
def initialize(students:, groups_of:, sort_type:)
  @students = students
  @groups_of = groups_of
  @sort_type = sort_type
  @final_groups = []
end

Public Instance Methods

check_student_distribution() click to toggle source
# File lib/learn_together/generator.rb, line 68
def check_student_distribution
  if leftover_students?
    leftover_val = students.length % groups_of.to_i
    if leftover_val == 1
      student = final_groups.pop.first
      final_groups.last << student
    elsif leftover_val == groups_of.to_i / 2
      final_groups.pop.each_with_index do |student, i|
        final_groups["-#{i + 1}".to_i] << student
      end
    end
  else
  end
end
form_progress_based_groups() click to toggle source
# File lib/learn_together/generator.rb, line 63
def form_progress_based_groups
  students.sort_by {|s| s.completed_lesson_count_for_active_track}.each_slice(groups_of.to_i) { |students| final_groups << students }
  check_student_distribution
end
form_random_groups() click to toggle source
# File lib/learn_together/generator.rb, line 58
def form_random_groups
  students.shuffle.each_slice(groups_of.to_i) { |students| final_groups << students }
   check_student_distribution
end
leftover_students() click to toggle source
# File lib/learn_together/generator.rb, line 87
def leftover_students
  students.length % groups_of.to_i <=  (groups_of.to_i - 2) || students.length % groups_of.to_i == 1
end
leftover_students?() click to toggle source
# File lib/learn_together/generator.rb, line 83
def leftover_students?
  students.length % groups_of.to_i > 0
end
make_groups() click to toggle source
# File lib/learn_together/generator.rb, line 40
def make_groups
  if sort_by_random?
    form_random_groups
  elsif sort_by_progress?
    form_progress_based_groups
  end
  final_groups
end
sort_by_progress?() click to toggle source
# File lib/learn_together/generator.rb, line 54
def sort_by_progress?
  sort_type == "progress"
end
sort_by_random?() click to toggle source
# File lib/learn_together/generator.rb, line 50
def sort_by_random?
  sort_type == "random"
end