Class: Generator::GroupsOfNStudents

Inherits:
Object
  • Object
show all
Defined in:
lib/learn_together/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(students:, groups_of:, sort_type:) ⇒ GroupsOfNStudents

Returns a new instance of GroupsOfNStudents.



32
33
34
35
36
37
# 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

Instance Attribute Details

#final_groupsObject

Returns the value of attribute final_groups.



30
31
32
# File 'lib/learn_together/generator.rb', line 30

def final_groups
  @final_groups
end

#groups_ofObject

Returns the value of attribute groups_of.



30
31
32
# File 'lib/learn_together/generator.rb', line 30

def groups_of
  @groups_of
end

#number_of_groupsObject

Returns the value of attribute number_of_groups.



30
31
32
# File 'lib/learn_together/generator.rb', line 30

def number_of_groups
  @number_of_groups
end

#sort_typeObject

Returns the value of attribute sort_type.



30
31
32
# File 'lib/learn_together/generator.rb', line 30

def sort_type
  @sort_type
end

#studentsObject

Returns the value of attribute students.



30
31
32
# File 'lib/learn_together/generator.rb', line 30

def students
  @students
end

Instance Method Details

#check_student_distributionObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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_groupsObject



63
64
65
66
# 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_groupsObject



58
59
60
61
# 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_studentsObject



87
88
89
# 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?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/learn_together/generator.rb', line 83

def leftover_students?
  students.length % groups_of.to_i > 0
end

#make_groupsObject



40
41
42
43
44
45
46
47
# 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?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/learn_together/generator.rb', line 54

def sort_by_progress?
  sort_type == "progress"
end

#sort_by_random?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/learn_together/generator.rb', line 50

def sort_by_random?
  sort_type == "random"
end