Skip to content

Instantly share code, notes, and snippets.

@gbrl
Forked from davidvandusen/candidates.rb
Last active April 28, 2016 19:43
Show Gist options
  • Save gbrl/f12ba1484358f0540d5feb962cccab17 to your computer and use it in GitHub Desktop.
Save gbrl/f12ba1484358f0540d5feb962cccab17 to your computer and use it in GitHub Desktop.
require 'active_support/all'
@candidates = [
{
id: 5,
years_of_experience: 4,
github_points: 293,
languages: ['C', 'Ruby', 'Python', 'Clojure'],
date_applied: 5.days.ago.to_date,
age: 26
},
{
id: 7,
years_of_experience: 1,
github_points: 145,
languages: ['JavaScript', 'Ruby', 'Go', 'Erlang'],
date_applied: 15.days.ago.to_date,
age: 19
},
{
id: 9,
years_of_experience: 6,
github_points: 435,
languages: ['JavaScript', 'SQL', 'C#'],
date_applied: 1.day.ago.to_date,
age: 32
},
{
id: 10,
years_of_experience: 3,
github_points: 232,
languages: ['Java', 'Ruby', 'JavaScript'],
date_applied: 12.days.ago.to_date,
age: 31
},
{
id: 11,
years_of_experience: 12,
github_points: 32,
languages: ['VB', 'Cobol', 'Fortran'],
date_applied: 2.days.ago.to_date,
age: 42
},
{
id: 13,
years_of_experience: 2,
github_points: 328,
languages: ['Python', 'Ruby', 'JavaScript'],
date_applied: 4.days.ago.to_date,
age: 25
},
{
id: 15,
years_of_experience: 1,
github_points: 400,
languages: ['JavaScript', 'Ruby'],
date_applied: 3.days.ago.to_date,
age: 16
},
]
# In this file we define the methods to help filter out candidates
# This way, we keep these methods separated from other potential parts of the program
def find(id)
results = @candidates.detect { |x| x[:id] == id }
end
def experienced?(candidate)
candidate[:years_of_experience] >= 2
end
def uses_github?(candidate)
candidate[:github_points] >= 100
end
def knows_ruby_or_python?(candidate)
["Ruby","Python"].any? { |lang| candidate[:languages].include? lang }
end
def applied_recently?(candidate)
candidate[:date_applied] >= 15.days.ago.to_date
end
def adult?(candidate)
candidate[:age] >= 18
end
def qualified?(candidate)
return true if experienced?(candidate) && uses_github?(candidate) && knows_ruby_or_python?(candidate) && applied_recently?(candidate) && adult?(candidate)
end
def qualified_candidates
@candidates.select do |person|
experienced?(person) && uses_github?(person) && knows_ruby_or_python?(person) && applied_recently?(person) && adult?(person)
end
end
def ordered_by_qualifications(candidates)
sorted = candidates.sort_by { |c| [ c[:years_of_experience], c[:github_points] ] }
sorted.reverse!
end
# More methods will go below
# This is the main entrypoint into the program
# It requires the other files/gems that it needs
require 'pry'
require 'hirb'
require './candidates'
require './filters'
## Your test code can go here
#binding.pry
def print_instructions
puts "Type 'find 1' to display candidate with id 1."
puts "Type 'all' to see all candidates."
puts "Type 'qualified' to print only qualified candidates."
puts "Type 'quit' to exit."
puts "Type 'help' to see these instructions again."
end
def colorize(text, color_code)
"\e[#{color_code}m#{text}\e[0m"
end
def red(text)
colorize(text, 31)
end
def green(text)
colorize(text, 32)
end
def all_candidates
results = []
@candidates.each do |c|
results << green(c) if qualified?(c)
results << red(c) unless qualified?(c)
end
puts results
end
def repl
puts "Welcome to your HR database. How can I help?"
print_instructions
keep_going = true
first_time = true
while keep_going
puts "Anything else? Type 'help' to see instructions." if first_time == false
first_time = false
response = gets.chomp.downcase
if response.match(/^find\s\d+/)
puts "Searching..."
id = response.match(/\d+/)
find(id[0].to_i)
elsif response == "quit"
keep_going = false
elsif response == "help"
print_instructions
elsif response == "qualified"
pp ordered_by_qualifications(qualified_candidates)
elsif response == "all"
all_candidates
else
puts "Sorry, we didn't understand your command."
end
end
end
#pp ordered_by_qualifications(qualified_candidates)
repl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment