-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.rb
95 lines (64 loc) · 1.85 KB
/
entry.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class Entry
attr_accessor :name, :email, :department
@@all = []
@@teams = {}
def initialize(name:, email:, department:)
self.name = name
self.email = email
self.department = department
@@all << self
end
def self.new_from_csv_entry(entry)
return if entry[0].nil?
Entry.new(name: "#{entry[1]} #{entry[0]}", department: entry[2], email: nil)
end
def self.randomize_by_department(num_of_teams)
entries_by_department = {}
# Seperating out the entries by department
Entry.all().each do |entry|
dept = entry.department
if entries_by_department[dept]
entries_by_department[dept].push(entry)
else
entries_by_department[dept] = [entry]
end
end
# Shuffling those new tables
entries_by_department.each do |key, value|
value.shuffle!
end
# Creating the teams
for i in 1..num_of_teams do
@@teams["team#{i}"] = []
end
# Assembling the teams
copy_of_entries = Marshal.load(Marshal.dump(entries_by_department))
i = 1
active_departments = self.get_active_departments(copy_of_entries)
while !active_departments.empty?
random_department = active_departments.keys.sample
while copy_of_entries[random_department].size > 0 do
i = 1 if i === num_of_teams + 1
random_entry = copy_of_entries[random_department].sample
@@teams["team#{i}"].push(random_entry)
copy_of_entries[random_department].delete(random_entry)
i += 1
end
# Update active_departments to reflect newly depleted department table
active_departments = self.get_active_departments(copy_of_entries)
end
end
def self.get_active_departments(departments)
active_departments = departments.select { |k, v| v.count > 0 }
active_departments
end
def self.teams
@@teams
end
def self.all
@@all
end
def to_s
puts "Name: #{self.name} - Email: #{self.email} - Department: #{self.department}"
end
end