Skip to content

Instantly share code, notes, and snippets.

@windsorbrown
Created April 4, 2016 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save windsorbrown/ecd872745aafc7da6a2234ec5341ee85 to your computer and use it in GitHub Desktop.
Save windsorbrown/ecd872745aafc7da6a2234ec5341ee85 to your computer and use it in GitHub Desktop.
candidates with exception
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)
raise '@candidates must be an Array' if @candidates.nil?
@candidates.each do |candidate|
if candidate[:id] == id
return candidate
end
end
nil
end
def experienced?(candidate)
candidate[:years_of_experience] >= 2
end
# More methods will go below
def github_points?(candidate)
candidate[:github_points] >= 100
end
def of_age?(candidate)
candidate[:age] > 17
end
def recently_applied?(candidate)
candidate[:date_applied] >= 15.days.ago.to_date
end
def expert_in_languages?(candidate)
candidate[:languages].include? "Ruby" || "Python"
end
def is_qualified?(candidate)
github_points?(candidate)&&
experienced?(candidate)&&
of_age?(candidate)&&
recently_applied?(candidate)&&
expert_in_languages?(candidate)
end
def print_qualified_candidates
ordered_by_qualifications.select do |candidate|
is_qualified?(candidate)
end
end
def ordered_by_qualifications
@candidates.sort_by do |candidate|
[candidate[:years_of_experience] , candidate[:github_points]].reverse
unless candidate.has_key?(:years_of_experience)
raise InvalidCandidateError, 'candidate must have a :years_of_experience key'
end
end
end
def list_candidates(option)
case option
when /find/
find(option.gsub!(/find\s/, '').to_i)
when "all"
ordered_by_qualifications
when "qualified"
print_qualified_candidates
else
"Please enter one of the options"
end
end
def show_menu
puts "find # : To find a candidate enter find + id number"
puts "all: To print all candidates who applied (one per line)"
puts "qualified: This will print only qualified candidates, ordered by experience and points (one per line)"
puts "quit: Exit the program"
end
loop do
show_menu
begin
option = gets.chomp
break if option == "quit"
puts list_candidates(option)
rescue InvalidCandidateError => ex
puts 'It could not be determined whether the candidate is experienced enough.'
puts "The reason was: #{ex.message}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment