Skip to content

Instantly share code, notes, and snippets.

@shinobcrc
Forked from davidvandusen/candidates.rb
Last active June 25, 2016 19:23
Show Gist options
  • Save shinobcrc/445647ea9edcb19484a5a1eff9791f4e to your computer and use it in GitHub Desktop.
Save shinobcrc/445647ea9edcb19484a5a1eff9791f4e 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
MIN_YEARS_EXP = 2
MIN_GITHUB_POINTS = 100
REQUIRED_LANGUAGES = ["Ruby", "Python"]
AGE = 17
APPLICATION_DATE = 15
def find(id)
@candidates.select do |candidate|
if @candidates[:id] == id
return id
else
return nil
end
end
def experienced?(candidate)
candidate[:years_of_experience] >= MIN_YEARS_EXP ? true : false
end
# More methods will go below
def github_points?(candidate)
candidate[:github_points] >= MIN_GITHUB_POINTS ? true : false
end
def language_knowledge?(candidate)
candidate[:languages].any? { |lanuage| REQUIRED_LANGUAGES.include? language}? true : false
end
def application_recent?(candidate)
candidate[:date_applied] >= Date.today - APPLICATION_DATE.days? true : false
end
def suitable_age?(candidate)
candidate[:age] >= AGE? true : false
end
# This is the main entrypoint into the program
# It requires the other files/gems that it needs
require 'pry'
require './candidates'
require './filters'
## Your test code can go here
def repl_menu
puts "You can find a candidate by searching using the following \n
search by using find with ID number
search by typing all
search by qualified to show only qualified candidates
type quit to exit"
end
def show_all
@candidates.each do |candidate|
if ordered_by_qualifications.include?(candidate)
pp candidate
else
pp candidate
puts "they are unqualified"
end
end
end
def show_qualified
pp ordered_by_qualifications
end
def show_id(input)
index = /\d/.match(input)
index = index.to_s.to_i + 1 - 1
pp find(index)
end
def qualified?(candidate)
experienced?(candidate) &&
github_points?(candidate) &&
language_knowledge(candidate) &&
application_recent(candidate) &&
suitable_age(candidate)
end
def ordered_by_qualifications
qualified_candidates = qualified?
ordered_by_qualifications = qualified_candidates.sort do |x,y|
if x[:years_of_experience] == y[:years_of_experience]
y[:github_points] <=> x [:github_points]
else
y[:years_of_experience] <=> x[:years_of_experience]
end
end
end
def input_menu
repl_menu
input = gets.chomp
while input != "quit"
if input =~ "\Afind"
show_id(input)
elsif input == "all"
show_all
elsif input == "qualified"
show_qualified
elsif input == "quit"
return
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment