parent
2105242e0a
commit
5e1715b0fc
@ -1,51 +0,0 @@ |
||||
module CryptCheck |
||||
class Status |
||||
LEVELS = %i(best perfect good warning error critical).freeze |
||||
PROBLEMS = %i(warning error critical).freeze |
||||
|
||||
extend Enumerable |
||||
|
||||
def self.each(&block) |
||||
LEVELS.each &block |
||||
end |
||||
|
||||
def self.empty |
||||
self.collect { |s| [s, []] }.to_h |
||||
end |
||||
|
||||
def self.status(statuses) |
||||
statuses = self.convert statuses |
||||
self.min LEVELS, statuses |
||||
end |
||||
|
||||
class << self |
||||
alias_method :'[]', :status |
||||
end |
||||
|
||||
def self.problem(statuses) |
||||
statuses = self.convert statuses |
||||
self.min PROBLEMS, statuses |
||||
end |
||||
|
||||
def self.sort(statuses) |
||||
statuses.sort { |a, b| self.compare a, b } |
||||
end |
||||
|
||||
def self.compare(a, b) |
||||
LEVELS.find_index(a.status) <=> LEVELS.find_index(b.status) |
||||
end |
||||
|
||||
private |
||||
def self.convert(statuses) |
||||
statuses = [statuses] unless statuses.respond_to? :first |
||||
first = statuses.first |
||||
statuses = statuses.collect &:status if first.respond_to? :status |
||||
statuses |
||||
end |
||||
|
||||
def self.min(levels, statuses) |
||||
return nil if statuses.empty? |
||||
(levels & statuses).last |
||||
end |
||||
end |
||||
end |
@ -1,48 +0,0 @@ |
||||
module CryptCheck |
||||
module Statused |
||||
def status |
||||
@status ||= calculate_status |
||||
end |
||||
|
||||
private |
||||
def merge(statuses) |
||||
Status.collect do |s| |
||||
status = statuses.collect { |ss| ss[s] } |
||||
status = status.inject &:+ |
||||
[s, status.uniq] |
||||
end.to_h |
||||
end |
||||
|
||||
def checks |
||||
[] |
||||
end |
||||
|
||||
def children |
||||
[] |
||||
end |
||||
|
||||
def perform_check(check) |
||||
name, check, level = check |
||||
result = check.call self |
||||
return nil unless result |
||||
level ||= result |
||||
[level, name] |
||||
end |
||||
|
||||
def personal_status |
||||
states = Status.empty |
||||
checks.each do |check| |
||||
level, name = perform_check check |
||||
next unless level |
||||
states[level] << name |
||||
end |
||||
states |
||||
end |
||||
|
||||
def calculate_status |
||||
children_statuses = children.collect(&:status) |
||||
statuses = [personal_status] + children_statuses |
||||
merge statuses |
||||
end |
||||
end |
||||
end |
@ -1,82 +0,0 @@ |
||||
describe CryptCheck::Statused do |
||||
def match_status(actual, **expected) |
||||
expected = ::CryptCheck::Status.empty.merge expected |
||||
expect(actual.status).to eq expected |
||||
end |
||||
|
||||
describe '::status' do |
||||
it 'must return empty if no check nor child' do |
||||
statused = Class.new do |
||||
include ::CryptCheck::Statused |
||||
end.new |
||||
match_status statused |
||||
end |
||||
|
||||
it 'must return personal status if no child' do |
||||
statused = Class.new do |
||||
include ::CryptCheck::Statused |
||||
|
||||
def checks |
||||
[ |
||||
[:foo, -> (_) { true }, :critical], |
||||
[:bar, -> (_) { :error }], |
||||
[:baz, -> (_) { false }] |
||||
] |
||||
end |
||||
end.new |
||||
match_status statused, critical: %i(foo), error: %i(bar) |
||||
end |
||||
|
||||
it 'must return personal and children statuses' do |
||||
child = Class.new do |
||||
include ::CryptCheck::Statused |
||||
|
||||
def checks |
||||
[[:bar, -> (_) { :error }]] |
||||
end |
||||
end.new |
||||
parent = Class.new do |
||||
include ::CryptCheck::Statused |
||||
|
||||
def initialize(child) |
||||
@child = child |
||||
end |
||||
|
||||
def checks |
||||
[[:foo, -> (_) { :critical }]] |
||||
end |
||||
|
||||
def children |
||||
[@child] |
||||
end |
||||
end.new(child) |
||||
match_status parent, critical: %i(foo), error: %i(bar) |
||||
end |
||||
|
||||
it 'must return remove duplicated status' do |
||||
child = Class.new do |
||||
include ::CryptCheck::Statused |
||||
|
||||
def checks |
||||
[[:foo, -> (_) { :critical }]] |
||||
end |
||||
end.new |
||||
parent = Class.new do |
||||
include ::CryptCheck::Statused |
||||
|
||||
def initialize(child) |
||||
@child = child |
||||
end |
||||
|
||||
def checks |
||||
[[:foo, -> (_) { :critical }]] |
||||
end |
||||
|
||||
def children |
||||
[@child] |
||||
end |
||||
end.new(child) |
||||
match_status parent, critical: %i(foo) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue