Compare/sort grades

aeris 7 months ago
parent 9b0571a7be
commit 20b223ea5a
  1. 22
      lib/cryptcheck/grade.rb

@ -1,6 +1,6 @@
module CryptCheck
module Grade
GRADES = %i(A+ A B+ B C+ C D E F G V T X)
GRADES = %i(A+ A B+ B C+ C D E F G V T X).freeze
GRADE_STATUS = {
:'A+' => :best,
A: :best,
@ -16,7 +16,7 @@ module CryptCheck
V: :critical,
T: :critical,
X: :critical
}
}.freeze
STATUS_GRADES = {
critical: :G,
error: :F,
@ -25,7 +25,7 @@ module CryptCheck
good: :C,
great: :B,
best: :A
}
}.freeze
def grade
@grade ||= calculate_grade
@ -34,5 +34,21 @@ module CryptCheck
def grade_status
GRADE_STATUS.fetch self.grade, :unknown
end
def self.compare(a, b)
GRADES.index(a.to_sym) <=> GRADES.index(b.to_sym)
end
def self.sort(grades)
grades.sort &self.method(:compare)
end
def self.better(grades)
self.sort(grades).first
end
def self.worst(grades)
self.sort(grades).last
end
end
end

Loading…
Cancel
Save