Class: Generator::GroupsOfNStudents

Inherits:
Object
  • Object
show all
Defined in:
lib/learn_party/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.



31
32
33
34
35
36
# File 'lib/learn_party/generator.rb', line 31

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.



29
30
31
# File 'lib/learn_party/generator.rb', line 29

def final_groups
  @final_groups
end

#groups_ofObject

Returns the value of attribute groups_of.



29
30
31
# File 'lib/learn_party/generator.rb', line 29

def groups_of
  @groups_of
end

#number_of_groupsObject

Returns the value of attribute number_of_groups.



29
30
31
# File 'lib/learn_party/generator.rb', line 29

def number_of_groups
  @number_of_groups
end

#sort_typeObject

Returns the value of attribute sort_type.



29
30
31
# File 'lib/learn_party/generator.rb', line 29

def sort_type
  @sort_type
end

#studentsObject

Returns the value of attribute students.



29
30
31
# File 'lib/learn_party/generator.rb', line 29

def students
  @students
end

Instance Method Details

#check_student_distributionObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/learn_party/generator.rb', line 67

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



62
63
64
65
# File 'lib/learn_party/generator.rb', line 62

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



57
58
59
60
# File 'lib/learn_party/generator.rb', line 57

def form_random_groups
  students.shuffle.each_slice(groups_of.to_i) { |students| final_groups << students }
   check_student_distribution
end

#leftover_studentsObject



86
87
88
# File 'lib/learn_party/generator.rb', line 86

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)


82
83
84
# File 'lib/learn_party/generator.rb', line 82

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

#make_groupsObject



39
40
41
42
43
44
45
46
# File 'lib/learn_party/generator.rb', line 39

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)


53
54
55
# File 'lib/learn_party/generator.rb', line 53

def sort_by_progress?
  sort_type == "progress"
end

#sort_by_random?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/learn_party/generator.rb', line 49

def sort_by_random?
  sort_type == "random"
end