Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
wattsworth
/
lumen-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5076e1c4
authored
May 23, 2016
by
John
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
refactored all extra classes into service folder
parent
42a89e45
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
24 additions
and
15 deletions
app/builders/db_builder.rb → app/services/db/update_db.rb
app/services/db_folder_agent.rb → app/services/folder/db_folder_agent.rb
app/services/create_nilm.rb → app/services/nilm/create_nilm.rb
config/application.rb
spec/builders/db_builder_spec.rb → spec/services/db/update_db_spec.rb
spec/services/db_folder_agent_spec.rb → spec/services/folder/db_folder_agent_spec.rb
spec/services/create_nilm_spec.rb → spec/services/nilm/create_nilm_spec.rb
app/
builders/db_builder
.rb
→
app/
services/db/update_db
.rb
View file @
5076e1c4
# 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
...
...
app/services/db_folder_agent.rb
→
app/services/
folder/
db_folder_agent.rb
View file @
5076e1c4
File moved
app/services/create_nilm.rb
→
app/services/
nilm/
create_nilm.rb
View file @
5076e1c4
...
...
@@ -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
config/application.rb
View file @
5076e1c4
...
...
@@ -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
spec/
builders/db_builder
_spec.rb
→
spec/
services/db/update_db
_spec.rb
View file @
5076e1c4
...
...
@@ -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
...
...
spec/services/db_folder_agent_spec.rb
→
spec/services/
folder/
db_folder_agent_spec.rb
View file @
5076e1c4
File moved
spec/services/create_nilm_spec.rb
→
spec/services/
nilm/
create_nilm_spec.rb
View file @
5076e1c4
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment