Commit 5076e1c4 by John

refactored all extra classes into service folder

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