Skip to content

Instantly share code, notes, and snippets.

@McTano
Forked from davidvandusen/candidates.rb
Last active June 7, 2016 16:41
Show Gist options
  • Save McTano/4fbc5d1c93af47d220d30efa6a223cb5 to your computer and use it in GitHub Desktop.
Save McTano/4fbc5d1c93af47d220d30efa6a223cb5 to your computer and use it in GitHub Desktop.
@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
class InvalidCandidateError < StandardError
end
def find(id)
@candidates.find {|candidate| candidate[:id] == id}
end
def experienced?(candidate)
raise InvalidCandidateError, "candidate must have a :years_of_experience key." unless candidate.has_key?(:years_of_experience)
return true if candidate[:years_of_experience] >= 2
return false
end
# More methods will go below
def qualified?(candidate)
experienced?(candidate) &&
candidate[:github_points] >= 100 &&
candidate[:languages].include?('Ruby') || candidate[:languages].include?('Python') &&
candidate[:date_applied] <= 15.days.ago.to_date &&
candidate[:age] > 17
end
def ordered_by_qualifications(candidates)
# candidates.sort do |a, b|
# experience_check = b[:years_of_experience] <=> a[:years_of_experience]
# experience_check != 0 ? experience_check : b[:github_points] <=> a[:github_points]
# end
candidates.sort_by {|a| [-a[:years_of_experience], -a[:github_points]]}
end
def colorize_by_experience(candidates)
candidates.each do |candidate|
experienced?(candidate) ? candidate.colorize(:green) : candidate.colorize(:red)
end
end
# def test_drive
# ordered_by_qualifications(@candidates)
# pp ordered_by_qualifications(qualified_candidates(candidates))
# end
# This is the main entrypoint into the program
# It requires the other files/gems that it needs
# require './candidates'
require './filters'
require 'pp'
require 'colorize'
require 'active_support/all'
## Your test code can go here
begin
raise ArgumentError, "@candidates must be an array" unless @candidates.is_a?(Array)
@qualified = @candidates.select {|candidate| qualified?(candidate)}
rescue ArgumentError => ex
puts "ERROR ", ex.message
end
# pp test_drive
loop do
pp "Enter command:"
puts "-\t 'find <id>' to view a candidate by id"
puts "-\t 'all' to view all candidates"
puts "-\t 'qualified' to view all candidates meeting our specifications"
puts "-\t 'quit' to quit"
# add list of commands
print "> "
input = gets.chomp.downcase
case input
when 'all'
ordered_by_qualifications(@candidates).each do
|candidate| puts candidate.to_s.colorize(qualified?(candidate) ? :green : :red)
end
# ordered_by_qualifications(unqualified).each do
# |candidate| puts candidate.to_s.colorize(:red)
# end
when 'qualified'
ordered_by_qualifications(@qualified).each {|candidate| puts candidate.to_s.colorize(:green)}
when 'quit'
break
else
if input.include?('find')
# binding.pry
result = find(input.delete('find').strip.to_i)
unless !result
puts result.to_s.colorize(qualified?(result) ? :green : :red)
else
pp "ID not found."
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment