Skip to content

Instantly share code, notes, and snippets.

@kaiguogit
Forked from davidvandusen/candidates.rb
Last active July 25, 2016 21:55
Show Gist options
  • Save kaiguogit/2374c105df4c60cfb961e052942e27f0 to your computer and use it in GitHub Desktop.
Save kaiguogit/2374c105df4c60cfb961e052942e27f0 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
class InvalidCandidateError < StandardError
end
def find(id)
raise '@candicates must be an Array' if @candidates.nil?
@candidates.select{|item| item[:id] == id}
end
def experienced?(candidate)
unless candidate.has_key?(:years_of_experience)
raise InvalidCandidateError, 'candidate must have a :years_of_experience key'
end
candidate[:years_of_experience] >= 2
end
# More methods will go below
def gitpoints100?(candidate)
unless candidate.has_key?(:github_points)
raise InvalidCandidateError, 'candidate must have a :github_points'
end
candidate[:github_points] >= 100
end
def know_ruby_or_python?(candidate)
unless candidate.has_key?(:languages)
raise InvalidCandidateError, 'candidate must have a :languages'
end
candidate[:languages].include?('Ruby') || candidate[:languages].include?('Python')
end
def age18?(candidate)
unless candidate.has_key?(:age)
raise InvalidCandidateError, 'candidate must have a :age'
end
candidate[:age] >= 18
end
def recently_applied?(candidate)
unless candidate.has_key?(:date_applied)
raise InvalidCandidateError, 'candidate must have a :date_applied'
end
candidate[:date_applied] > 15.days.ago.to_date
end
def qualified_candidates(candidates)
candidates.select{|candidate| experienced?(candidate) && gitpoints100?(candidate) && know_ruby_or_python?(candidate) && age18?(candidate) && recently_applied?(candidate)
}
end
def ordered_by_qualification(candidates)
candidates.sort do |a,b|
unless a.has_key?(:years_of_experience)
raise InvalidCandidateError, 'candidate must have a :years_of_experience key'
end
unless a.has_key?(:github_points)
raise InvalidCandidateError, 'candidate must have a :github_points'
end
unless b.has_key?(:years_of_experience)
raise InvalidCandidateError, 'candidate must have a :years_of_experience key'
end
unless b.has_key?(:github_points)
raise InvalidCandidateError, 'candidate must have a :github_points'
end
if a[:years_of_experience] < b[:years_of_experience]
1
elsif a[:years_of_experience] == b[:years_of_experience]
b[:github_points] <=> a[:github_points]
else
-1
end
end
end
# This is the main entrypoint into the program
# It requires the other files/gems that it needs
require 'pry'
require './candidates'
require './filters'
require 'pp'
## Your test code can go here
# binding.pry
#@candidates.each { |x| puts experienced?(x)}
#puts find(5)
#pp qualified_candidates(@candidates)
#pp ordered_by_qualification(@candidates)
def get_input
print ">"
gets
end
def clear
system("clear")
puts "Enter find id number to find candidate by id"
puts "Enter all to list all"
puts "Enter qualified to list all qualified candidates"
puts "Enter quit to leave"
end
def main
begin
output = decode_user_input(get_input.strip.downcase)
clear
if output.class ==Array
output.each do |cdt|
qualified_candidates(@candidates).include?(cdt) ? (puts green(hash_to_str(cdt))) : (puts red(hash_to_str(cdt)))
end
elsif output.class == String
puts output
end
end while output != "quit"
system("clear")
end
def decode_user_input(command)
case command
when /find \w/
id = command.match(/find (\w+)/).captures[0].to_i
result = find(id)
result == [] ? "Cannot find candidate with ID #{id}" : result
when "all"
@candidates
when "qualified"
ordered_by_qualification(qualified_candidates(@candidates))
when "quit"
"quit"
else
"invalid input"
end
end
def hash_to_str(hash)
a = "Canadate: ID: #{hash[:id]}, Experience: #{hash[:years_of_experience]}, "
b = "GithubPoint: #{hash[:github_points]}, "
c = "Languages: #{hash[:languages].join(", ")}, "
d = "Date Applied: #{hash[:date_applied]}, "
e = "Age: #{hash[:age]}"
a + b + c + d + e
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
#puts find(5)
clear
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment