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.

status.rb 975B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.status(statuses)
  10. statuses = self.convert statuses
  11. self.min LEVELS, statuses
  12. end
  13. class << self
  14. alias_method :'[]', :status
  15. end
  16. def self.problem(statuses)
  17. statuses = self.convert statuses
  18. self.min PROBLEMS, statuses
  19. end
  20. def self.sort(statuses)
  21. statuses.sort { |a, b| self.compare a, b }
  22. end
  23. def self.compare(a, b)
  24. LEVELS.find_index(a.status) <=> LEVELS.find_index(b.status)
  25. end
  26. private
  27. def self.convert(statuses)
  28. statuses = [statuses] unless statuses.respond_to? :first
  29. first = statuses.first
  30. statuses = statuses.collect &:status if first.respond_to? :status
  31. statuses
  32. end
  33. def self.min(levels, statuses)
  34. return nil if statuses.empty?
  35. (levels & statuses).last
  36. end
  37. end
  38. end