Commit 3720f9af by John Doe

added service_status module

parent 0148696b
......@@ -2,12 +2,11 @@
# Handles construction of database objects
class UpdateDb
attr_accessor :warnings, :errors
include ServiceStatus
def initialize(db:)
@db = db
@warnings = []
@errors = []
super()
end
def run(db_adapter:)
......@@ -19,16 +18,10 @@ class UpdateDb
entries = __create_entries(db_adapter.schema)
updater = UpdateFolder.new(@root_folder, entries)
updater.run
@errors << updater.errors
@warnings << updater.warnings
# parse the entries array
# Note: @root_folder gets linked in on
# the first call to __build_folder
# Don't save the result if there were errors
return false unless @errors.empty?
absorb_status(updater.run)
@db.save
self
end
protected
......
......@@ -2,18 +2,18 @@
# Handles construction of DbFolder objects
class UpdateFile
attr_accessor :warnings, :errors
include ServiceStatus
def initialize(file, base_entry, decimation_entries)
@file = file
@base_entry = base_entry
@decimation_entries = decimation_entries
@warnings = []
@errors = []
super()
end
def run
__update_file(@file, @base_entry, @decimation_entries)
self
end
# regex matching the ~decimXX ending on a stream path
......
......@@ -2,13 +2,12 @@
# Handles construction of DbFolder objects
class UpdateFolder
attr_accessor :warnings, :errors
include ServiceStatus
def initialize(folder, entries)
@folder = folder
@entries = entries
@warnings = []
@errors = []
super()
end
# returns the updated DbFolder object
......@@ -21,6 +20,7 @@ class UpdateFolder
__parse_folder_entries(@folder, @entries)
# save the result
@folder.save!
self
end
protected
......@@ -85,9 +85,7 @@ class UpdateFolder
else # its a folder
updater = __build_folder(folder, entry_group, name)
end
updater.run
@warnings << updater.warnings
@errors << updater.errors
absorb_status(updater.run)
end
end
......
......@@ -9,7 +9,7 @@ class CreateNilm
@warnings = []
end
def build(name:, url:, description: '')
def run(name:, url:, description: '')
# create the NILM object
@nilm = Nilm.new(name: name, url: url,
description: description)
......
# frozen_string_literal: true
# Handles service errors and warnings. Design pattern:
# All service action should occur in the run() function
# Within run, call add_error or add_warning with string
# messages. At the end of run() return the service object itself
# For nested services, call absorb_service(child.run()) to nest
# the errors and warnings of the child into the parent, set
# the action parameter to NEVER_FAIL, FAIL_ON_WARNING, or
# FAIL_ON_ERROR to determine when (if ever), absorb_status
# returns false
module ServiceStatus
attr_reader :errors, :warnings
FAIL_ON_ERROR = 0
FAIL_ON_WARNING = 1
NEVER_FAIL = 2
def initialize
@errors = []
@warnings = []
end
def add_error(message)
@errors << String(message)
end
def errors?
!@errors.empty?
end
def add_warning(message)
@warnings << String(message)
end
def warnings?
!@warnings.empty?
end
def run
raise 'Implement in client, return service object'
end
def absorb_status(service, action: FAIL_ON_ERROR)
@warnings += service.warnings
@errors += service.errors
case action
when FAIL_ON_WARNING
return false if warnings? || errors?
when FAIL_ON_ERROR
return false if errors?
end
true
end
end
......@@ -11,7 +11,7 @@ RSpec.describe 'CreateNilm' do
allow(UpdateDb).to receive(:new).and_return(service)
# run the NILM creation
nilm_creator = CreateNilm.new
nilm_creator.build(name: 'test', url: test_nilm_url)
nilm_creator.run(name: 'test', url: test_nilm_url)
# verify NILM components are present
expect(nilm_creator.nilm).to be_present
expect(nilm_creator.nilm.db).to be_present
......
# frozen_string_literal: true
require 'rails_helper'
class ModuleTester
include ServiceStatus
def initialize
@x = 2
super
end
end
describe 'ServiceStatus' do
it 'tracks errors' do
x = ModuleTester.new
x.add_error('message')
expect(x.errors?).to be true
expect(x.errors.length).to eq(1)
end
it 'tracks warnings' do
x = ModuleTester.new
x.add_warning('message')
expect(x.warnings?).to be true
expect(x.warnings.length).to eq(1)
end
it 'raises error if *run* is not implemented' do
x = ModuleTester.new
expect { x.run }.to raise_error(RuntimeError)
end
describe 'absorb_status' do
let(:parent) { ModuleTester.new }
let(:child) { ModuleTester.new }
it 'adds errors/warnings from service into itself' do
child.add_error('test')
child.add_warning('test')
parent.absorb_status(child)
expect(parent.errors?).to be true
expect(parent.warnings?).to be true
end
it 'it always returns true if action==NEVER_FAIL' do
child.add_error('test')
child.add_warning('test')
expect(
parent.absorb_status(child,
action: ServiceStatus::NEVER_FAIL))
.to be true
end
it 'fails on errors and warnings with FAIL_ON_WARNING' do
child.add_warning('test')
expect(
parent.absorb_status(child,
action: ServiceStatus::FAIL_ON_WARNING))
.to be false
end
it 'fails on errors with FAIL_ON_ERROR' do
child.add_error('test')
# default action
expect(parent.absorb_status(child)).to be false
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment