Commit 5076e1c4 by John

refactored all extra classes into service folder

parent 42a89e45
# frozen_string_literal: true
# Handles construction of database objects
class DbBuilder
class UpdateDb
def initialize(db:)
@db = db
end
def update_db(schema:)
def run(db_adapter:)
# create the root folder if it doesn't exist
@db.root_folder ||= DbFolder.create(name: 'root', path: '/')
@root_folder = @db.root_folder
# create the entry array from the schema
entries = __create_entries(schema)
entries = __create_entries(db_adapter.schema)
# parse the entries array
# Note: @root_folder gets linked in on
......
......@@ -10,12 +10,14 @@ class CreateNilm
end
def build(name:, url:, description: '')
# create the NILM object
@nilm = Nilm.new(name: name, url: url,
description: description)
@nilm.save
# create the database object and update it
db = Db.create(nilm: @nilm)
builder = DbBuilder.new(db: db)
service = UpdateDb.new(db: db)
adapter = DbAdapter.new(db.url)
builder.update_db(schema: adapter.schema)
service.run(db_adapter: adapter)
end
end
......@@ -22,5 +22,10 @@ module ControlPanel
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
# Add folders under the services directory
['nilm','db','folder'].each do |service|
config.autoload_paths << Rails.root.join("app/services/#{service}")
end
end
end
......@@ -21,12 +21,16 @@ simple_db = [
helper.entry('/folder2/f2_2', metadata: { name: 'file2_2' })
]
describe DbBuilder do
describe '*update_db*' do
describe 'UpdateDb' do
describe '*run*' do
def update_with_schema(schema)
# stub the database adapter
adapter = instance_double(DbAdapter)
allow(adapter).to receive(:schema).and_return(Array.new(schema))
# run the update
@db = Db.new
@db_builder = DbBuilder.new(db: @db)
@db_builder.update_db(schema: Array.new(schema))
@service = UpdateDb.new(db: @db)
@service.run(db_adapter: adapter)
@root = @db.root_folder
end
describe 'given the simple_db schema' do
......
......@@ -6,11 +6,9 @@ test_nilm_url = 'http://nilm.secondary'
RSpec.describe 'CreateNilm' do
describe 'build' do
it 'creates and populates a Db object' do
# mock the database builder
builder = instance_double(DbBuilder, update_db: '')
allow(DbBuilder).to receive(:new).and_return(builder)
adapter = instance_double(DbAdapter, schema: '')
allow(DbAdapter).to receive(:new).and_return(adapter)
# mock the database updater
service = instance_double(UpdateDb, run: '')
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)
......@@ -18,7 +16,7 @@ RSpec.describe 'CreateNilm' do
expect(nilm_creator.nilm).to be_present
expect(nilm_creator.nilm.db).to be_present
# ...and the database has been populated
expect(builder).to have_received(:update_db)
expect(service).to have_received(:run)
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