Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. module CryptCheck
  2. module State
  3. def states
  4. @status ||= calculate_states
  5. end
  6. def status
  7. State.status self.states.reject { |_, v| v.empty? }.keys
  8. end
  9. LEVELS = %i(best perfect good warning error critical).freeze
  10. PROBLEMS = %i(warning error critical).freeze
  11. extend Enumerable
  12. def self.each(&block)
  13. LEVELS.each &block
  14. end
  15. def self.empty
  16. self.collect { |s| [s, []] }.to_h
  17. end
  18. def self.status(states)
  19. states = self.convert states
  20. self.min LEVELS, states
  21. end
  22. class << self
  23. alias_method :'[]', :status
  24. end
  25. def self.problem(states)
  26. states = self.convert states
  27. self.min PROBLEMS, states
  28. end
  29. def self.sort(states)
  30. states.sort { |a, b| self.compare a, b }
  31. end
  32. def self.compare(a, b)
  33. LEVELS.find_index(a.status) <=> LEVELS.find_index(b.status)
  34. end
  35. def performed_checks
  36. self.states # Force internal resolution
  37. @performed_checks
  38. end
  39. private
  40. def self.convert(status)
  41. status = [status] unless status.respond_to? :first
  42. first = status.first
  43. status = status.collect &:status if first.respond_to? :status
  44. status
  45. end
  46. def self.min(levels, states)
  47. return nil if states.empty?
  48. (levels & states).last
  49. end
  50. def self.merge(*states)
  51. State.collect do |s|
  52. state = states.collect { |ss| ss.fetch s, [] }
  53. .inject(&:+).uniq
  54. [s, state]
  55. end.to_h
  56. end
  57. def children
  58. []
  59. end
  60. def perform_check(check)
  61. name, check, level = check
  62. result = check.call self
  63. return nil unless result
  64. level ||= result
  65. [level, name]
  66. end
  67. def personal_states
  68. states = State.empty
  69. performed_checks = checks
  70. performed_checks.each do |check|
  71. level, name = perform_check check
  72. next unless level
  73. states[level] << name
  74. end
  75. performed_checks = [
  76. performed_checks
  77. .collect { |n, _, l| [l, n] }
  78. .group_by(&:first)
  79. .map { |k, v| [k, v.collect(&:last)] }.to_h
  80. ] + children.collect(&:performed_checks)
  81. @performed_checks = State.merge *performed_checks
  82. states
  83. end
  84. def calculate_states
  85. children_states = children.collect(&:states)
  86. states = [personal_states] + children_states
  87. State.merge *states
  88. end
  89. end
  90. end