parent
c1241eae97
commit
13fa9541d6
@ -0,0 +1,48 @@ |
||||
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 |
@ -0,0 +1,82 @@ |
||||
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