Last active
August 29, 2015 14:09
-
-
Save JesseHerrick/8cc17ea51ef8f3a3c0a0 to your computer and use it in GitHub Desktop.
A secret Santa name assigner.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
// fusion | |
"Ashlynn Broedling", | |
"Ben Phipps", | |
"Ben Reineke", | |
"Danni McClendon", | |
"Eric Turner", | |
"Imani Terrell", | |
"Jean Joo", | |
"Jesse Herrick", | |
"Kaleb Murdock", | |
"Katie Breslin", | |
"Luke Sheidler", | |
"Maddie Moore", | |
"Mark Howard", | |
"Matthew Koehler", | |
"Megan Good", | |
"Phil Rupp", | |
"Seth Eggenschwiller", | |
"Taylor Siebert" | |
"Trevor Ginsberg", | |
// eleventh hour | |
"Brody McDonald", | |
"Dani Carrington", | |
"Libby Groll", | |
"Lilly Eggenschwiller", | |
"Alex Wunder", | |
"Daniel Pedro", | |
"Sam Gyenes", | |
"Rachel Dunn", | |
"Joe Schlangen" | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
class Santa | |
attr_accessor :list | |
def initialize(names) | |
self.list = {} | |
@names = names | |
# compile list of names | |
pick_all do |name| | |
pick_name name | |
end | |
end | |
def find(name) | |
name_assignment = self.list.keys.select { |key| key.match /#{name}/i } | |
if name_assignment.count > 1 | |
name_assignment.each_with_index do |n, i| | |
print "Are you #{n}? (y/n) " | |
if $stdin.gets.chomp.match(/y/i) | |
# return final assignment | |
return self.list[name_assignment[i]] | |
else | |
next | |
end | |
end | |
elsif name_assignment.count == 0 | |
puts "I'm sorry, #{name}. You're not on the list." | |
else | |
return self.list[name_assignment.first] | |
end | |
end | |
def pick_name(name, names = @names, picked = self.list) | |
partner = names.sample | |
if partner == name | |
pick_name(name) | |
elsif picked[name] | |
picked[name] | |
elsif picked.values.include? partner | |
pick_name(name) | |
else | |
picked[name] = partner | |
end | |
{ name => picked[name] } | |
end | |
def pick_all | |
@names.each { |name| yield name } | |
end | |
def to_json | |
self.list.to_json | |
end | |
# the cli | |
def cli | |
trap('SIGINT') { exit } | |
print "Type your name: " | |
@name = $stdin.gets.chomp | |
cli if @name.empty? | |
puts "Assigned to: #{find(@name) || 'Nobody :('}" | |
puts "Press ENTER when ready." | |
$stdin.gets | |
print `clear` | |
cli | |
end | |
end | |
cast = JSON.parse File.read('cast.json') | |
santa = Santa.new(cast) | |
santa.cli |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment