Class: Pairity::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/pairity/cli.rb

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



8
9
10
11
12
13
14
# File 'lib/pairity/cli.rb', line 8

def initialize
  @matrix = AdjacencyMatrix.new
  @sync = GoogleSync.new(@matrix)
  @generator = PairGenerator.new(@matrix)
  @renderer = PairRenderer.new(@matrix)
  load_google
end

Instance Method Details

#action_menuObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pairity/cli.rb', line 24

def action_menu
  puts
  puts Rainbow("==== PAIRITY ====").white
  choose do |menu|
    menu.prompt = "What would you like to do?"
    menu.choice("Generate Pairs") { generate_pairs }
    menu.choice("Edit People") { edit_people }
    # menu.choice("Edit Pair") { edit_pair }
    menu.choice("Save Changes") { save_changes }
    menu.choice("Open Google Sheet") { open_google_sheet }
    menu.choice("Change Slack Channel") { change_channel }
  end
end

#add_peopleObject



150
151
152
153
154
155
156
157
158
159
# File 'lib/pairity/cli.rb', line 150

def add_people
  names = ask "What are their names? (enter names separated by commas)"
  names = names.split(",")
  names.each do |name|
    person = Person.new(name: name.strip)
    @matrix.add_person(person)
    puts "Added #{name.strip}!"
  end
  action_menu
end

#all_combosObject



298
299
300
# File 'lib/pairity/cli.rb', line 298

def all_combos
  @matrix.people.combination(2)
end

#change_channelObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pairity/cli.rb', line 57

def change_channel
  puts "Let's set up Slack Integration."
  Config.load

  channel = ask("What channel would you like to post to? (Don't write the #')")

  Config.add(channel: channel)
  Config.save
  puts "Channel changed to ##{channel}"
  action_menu
end

#change_tierObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pairity/cli.rb', line 97

def change_tier
  choice = choose_person(tier: true)

  tier = 2
  choose do |menu|
    menu.prompt = "Set #{Rainbow(choice.name).white} to what tier?"
    menu.choice("Tier 1") { tier = 1 }
    menu.choice("Tier 2") { tier = 2 }
    menu.choice("Tier 3") { tier = 3 }
  end

  choice.tier = tier

  puts "#{Rainbow(choice.name).white} is now tier #{tier}."

  action_menu
end

#choose_from_generated_pairObject



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/pairity/cli.rb', line 178

def choose_from_generated_pair
  choice = nil

  choose do |menu|
    menu.prompt = "Who would you like to edit?"
    @generator.pairs.each_with_index do |pair, index|
      menu.choice(display_pair_names(pair)) { choice = index }
    end
  end

  choice = @generator.pairs[choice]
end

#choose_pairObject



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/pairity/cli.rb', line 191

def choose_pair
  choice = nil

  choose do |menu|
    menu.prompt = "Who would you like to edit?"
    all_combos.with_index do |pair, index|
      menu.choice(display_pair_names(pair)) { choice = index }
    end
  end

  choice = all_combos.to_a[choice]
end

#choose_person(tier: false) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pairity/cli.rb', line 131

def choose_person(tier: false)
  people = @matrix.people(true)
  choice = nil
  choose do |menu|
    menu.prompt = "Select a person."
    people.each_with_index do |person, index|
      menu.choice(display_person(person, tier: tier)) { choice = people[index] }
    end
  end
  choice
end

#condemn_pair(pair) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/pairity/cli.rb', line 245

def condemn_pair(pair)

  answer = ask "Are you sure you would like to condemn #{display_pair_names(pair)}?" do |q|
    q.validate = /y|n/
  end

  if answer =~ /y/
    @generator.abolish_pairing(*pair)
    puts "#{display_pair_names(pair)} will no longer be paired."
  end

  action_menu
end

#decrease_chance(pair) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/pairity/cli.rb', line 231

def decrease_chance(pair)
  answer = ask "Are you sure you would like to decrease the odds of #{display_pair_names(pair)} pairing?" do |q|
    q.validate = /y|n/
  end

  if answer =~ /y/
    p1, p2 = pair
    @generator.resistance(p1, p2, 2)
    puts "#{display_pair_names(pair)} are less likely to be paired."
  end

  action_menu
end

#display_pair(rows, person1, person2) ⇒ Object



325
326
327
328
329
330
# File 'lib/pairity/cli.rb', line 325

def display_pair(rows, person1, person2)
  cols = []
  cols << display_pair_names([person1, person2])
  cols << @renderer.stats_for_pair(person1, person2)
  rows << cols
end

#display_pair_names(pair) ⇒ Object



320
321
322
323
# File 'lib/pairity/cli.rb', line 320

def display_pair_names(pair)
  person1, person2 = pair
  Rainbow(person1).white + " & " + Rainbow(person2).white
end

#display_pair_statsObject



302
303
304
305
306
307
308
309
# File 'lib/pairity/cli.rb', line 302

def display_pair_stats
  rows = []
  all_combos.each do |person1, person2|
    display_pair(rows, person1, person2)
  end
  table = Terminal::Table.new headings: ["Pair","Times"], rows: rows
  puts table
end

#display_pairsObject



311
312
313
314
315
316
317
318
# File 'lib/pairity/cli.rb', line 311

def display_pairs
  rows = []
  @generator.pairs.each do |person1, person2|
    display_pair(rows, person1, person2)
  end
  table = Terminal::Table.new headings: ["Pair","Times"], rows: rows
  puts table
end

#display_person(person, tier: false) ⇒ Object



143
144
145
146
147
148
# File 'lib/pairity/cli.rb', line 143

def display_person(person, tier: false)
  output = []
  output << "#{Rainbow(person.name).white}"
  output << "-- Tier #{person.tier}" if tier
  output.join(" ")
end

#edit_pairObject



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/pairity/cli.rb', line 205

def edit_pair
  choice = choose_pair

  choose do |menu|
    menu.prompt = "How would you like to edit #{display_pair_names(choice)}"
    menu.choice("Increase Pair Chance") { increase_chance(choice) }
    menu.choice("Decrease Pair Chance") { decrease_chance(choice) }
    menu.choice("Abolish Pair Chance") { condemn_pair(choice) }
  end

end

#edit_peopleObject



69
70
71
72
73
74
75
76
77
# File 'lib/pairity/cli.rb', line 69

def edit_people
  choose do |menu|
    menu.prompt = "Add or remove?"
    menu.choice("Add") { add_people }
    menu.choice("Remove") { remove_people }
    menu.choice("Rename") { rename }
    menu.choice("Change Tier") { change_tier }
  end
end

#generate_pairsObject



259
260
261
262
263
# File 'lib/pairity/cli.rb', line 259

def generate_pairs
  @generator.generate_pairs
  puts
  pairs_menu
end

#increase_chance(pair) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/pairity/cli.rb', line 217

def increase_chance(pair)
  answer = ask "Are you sure you would like to increase the odds of #{display_pair_names(pair)} pairing?" do |q|
    q.validate = /y|n/
  end

  if answer =~ /y/
    p1, p2 = pair
    @generator.resistance(p1, p2, 0.5)
    puts "#{display_pair_names(pair)} are more likely to be paired."
  end

  action_menu
end

#load_googleObject



332
333
334
# File 'lib/pairity/cli.rb', line 332

def load_google
  @sync.load
end

#nope_pairObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/pairity/cli.rb', line 161

def nope_pair
  pair = choose_from_generated_pair

  answer = ask "Are you sure you would like to nope today's #{display_pair_names(pair)} pairing?" do |q|
    q.validate = /y|n/
  end

  if answer =~ /y/
    p1, p2 = pair
    @generator.nope(p1, p2)
    puts "#{display_pair_names(pair)} have been 'Noped'!"
  end

  @generator.generate_pairs
  pairs_menu
end

#open_google_sheetObject



38
39
40
41
42
# File 'lib/pairity/cli.rb', line 38

def open_google_sheet
  `open "#{@sync.sheet_url}"`

  action_menu
end

#pairs_menuObject



265
266
267
268
269
270
271
272
273
274
# File 'lib/pairity/cli.rb', line 265

def pairs_menu
  display_pairs
  puts
  choose do |menu|
    menu.prompt = "What would you like to do?"
    menu.choice("Save & Slack") { save_pairs }
    menu.choice("'Nope' a Pair") { nope_pair }
    menu.choice("Main Menu") { action_menu }
  end
end

#remove_peopleObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pairity/cli.rb', line 115

def remove_people
  choice = choose_person

  answer = ask "Are you sure you would like to remove #{Rainbow(choice).white}?" do |q|
    q.validate = /y|n/
  end

  if answer =~ /y/
    @matrix.remove_person(choice)
  end

  puts "#{Rainbow(choice).white} has been removed."

  action_menu
end

#renameObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pairity/cli.rb', line 84

def rename
  choice = choose_person

  new_name = ask "What name would you like to give #{Rainbow(choice.name).white}?"

  old_name = choice.name
  choice.name = new_name

  puts "#{Rainbow(old_name).white} shall henceforth be known as #{Rainbow(choice.name).white}!"

  action_menu
end

#save_changesObject



79
80
81
82
# File 'lib/pairity/cli.rb', line 79

def save_changes
  @sync.save
  action_menu
end

#save_pairsObject



276
277
278
279
280
281
282
283
# File 'lib/pairity/cli.rb', line 276

def save_pairs
  @generator.save_pairs
  puts
  PairSaver.new(@generator.pairs).post_to_slack
  say "Posted pairings to slack!"
  @sync.save
  action_menu
end

#simulate_pairsObject



285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/pairity/cli.rb', line 285

def simulate_pairs
  times = ask("How many days should we to travel?")

  times.to_i.times do
    @generator.generate_pairs
    @generator.save_pairs
  end

  display_pair_stats

  action_menu
end

#slack_configObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pairity/cli.rb', line 44

def slack_config
  puts "Let's set up Slack Integration."
  Config.load
  slack_webhook = ask("Please enter your Slack Webhook URL (more information: https://vikingcodeschool.slack.com/apps/new/A0F7XDUAZ-incoming-webhooks)") do |q|
    q.validate = /hooks\.slack\.com\/services\//
  end

  channel = ask("What channel would you like to post to? (Don't write the #')")

  Config.add(url: slack_webhook, channel: channel)
  Config.save
end

#startObject



16
17
18
19
20
21
22
# File 'lib/pairity/cli.rb', line 16

def start
  unless Config.configured?
    slack_config
  end

  action_menu
end