You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.0KB

  1. module CryptCheck
  2. class Status
  3. LEVELS = %i(best perfect good warning error critical).freeze
  4. PROBLEMS = %i(warning error critical).freeze
  5. extend Enumerable
  6. def self.each(&block)
  7. LEVELS.each &block
  8. end
  9. def self.empty
  10. self.collect { |s| [s, []] }.to_h
  11. end
  12. def self.status(statuses)
  13. statuses = self.convert statuses
  14. self.min LEVELS, statuses
  15. end
  16. class << self
  17. alias_method :'[]', :status
  18. end
  19. def self.problem(statuses)
  20. statuses = self.convert statuses
  21. self.min PROBLEMS, statuses
  22. end
  23. def self.sort(statuses)
  24. statuses.sort { |a, b| self.compare a, b }
  25. end
  26. def self.compare(a, b)
  27. LEVELS.find_index(a.status) <=> LEVELS.find_index(b.status)
  28. end
  29. private
  30. def self.convert(statuses)
  31. statuses = [statuses] unless statuses.respond_to? :first
  32. first = statuses.first
  33. statuses = statuses.collect &:status if first.respond_to? :status
  34. statuses
  35. end
  36. def self.min(levels, statuses)
  37. return nil if statuses.empty?
  38. (levels & statuses).last
  39. end
  40. end
  41. end