Commit 42a89e45 by John Doe

working on database parsing

parent 8e58fdd5
...@@ -18,6 +18,7 @@ class DbBuilder ...@@ -18,6 +18,7 @@ class DbBuilder
# Note: @root_folder gets linked in on # Note: @root_folder gets linked in on
# the first call to __build_folder # the first call to __build_folder
__parse_folder_entries(parent: nil, entries: entries) __parse_folder_entries(parent: nil, entries: entries)
@db.save
end end
protected protected
......
# frozen_string_literal: true
# controller for NILM objects
class NilmsController < ApplicationController
end
...@@ -3,4 +3,20 @@ ...@@ -3,4 +3,20 @@
# Database object # Database object
class Db < ActiveRecord::Base class Db < ActiveRecord::Base
belongs_to :root_folder, foreign_key: 'db_folder_id', class_name: 'DbFolder' belongs_to :root_folder, foreign_key: 'db_folder_id', class_name: 'DbFolder'
belongs_to :nilm
def url
# return a custom URL if set
return @url unless @url.nil? || @url.empty?
# no default URL if no parent NILM available
return '--error, no parent NILM--' if nilm.nil?
# return the default URL"
"#{nilm.url}/nilmdb"
end
def as_json(options = {})
db = super(except: [:created_at, :updated_at])
db[:contents] = root_folder.as_json(options)
db
end
end end
...@@ -10,4 +10,8 @@ class DbFile < ActiveRecord::Base ...@@ -10,4 +10,8 @@ class DbFile < ActiveRecord::Base
db_service.remove_file(path) db_service.remove_file(path)
destroy destroy
end end
def as_json(_options = {})
super(except: [:created_at, :updated_at])
end
end end
...@@ -14,4 +14,11 @@ class DbFolder < ActiveRecord::Base ...@@ -14,4 +14,11 @@ class DbFolder < ActiveRecord::Base
return false unless file.valid? return false unless file.valid?
true true
end end
def as_json(_options = {})
folder = super(except: [:created_at, :updated_at])
folder[:subfolders] = subfolders.map(&:as_json)
folder[:files] = db_files.map(&:as_json)
folder
end
end end
# frozen_string_literal: true
# NILM object
class Nilm < ActiveRecord::Base
has_one :db
end
# frozen_string_literal: true
# Agent class for DbFolders
class CreateNilm
attr_accessor :errors, :warnings, :nilm
def initialize
@errors = []
@warnings = []
end
def build(name:, url:, description: '')
@nilm = Nilm.new(name: name, url: url,
description: description)
@nilm.save
db = Db.create(nilm: @nilm)
builder = DbBuilder.new(db: db)
adapter = DbAdapter.new(db.url)
builder.update_db(schema: adapter.schema)
end
end
Rails.application.routes.draw do Rails.application.routes.draw do
resources :nilms
resources :dbs resources :dbs
resources :db_streams resources :db_streams
resources :db_files resources :db_files
......
class CreateNilms < ActiveRecord::Migration
def change
create_table :nilms do |t|
t.string :name
t.string :description
t.string :url
t.timestamps null: false
end
add_column :dbs, :nilm_id, :integer
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160520145007) do ActiveRecord::Schema.define(version: 20160522180603) do
create_table "db_files", force: :cascade do |t| create_table "db_files", force: :cascade do |t|
t.string "name" t.string "name"
...@@ -49,6 +49,15 @@ ActiveRecord::Schema.define(version: 20160520145007) do ...@@ -49,6 +49,15 @@ ActiveRecord::Schema.define(version: 20160520145007) do
t.integer "db_folder_id" t.integer "db_folder_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "nilm_id"
end
create_table "nilms", force: :cascade do |t|
t.string "name"
t.string "description"
t.string "url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end end
end end
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe NilmsController, type: :controller do
end
# frozen_string_literal: true
FactoryGirl.define do
factory :nilm do
end
end
...@@ -2,15 +2,15 @@ ...@@ -2,15 +2,15 @@
require 'rails_helper' require 'rails_helper'
helper = DbSchemaHelper.new # helper = DbSchemaHelper.new
simple_db = [ # simple_db = [
helper.entry('/folder1/f1_1', # helper.entry('/folder1/f1_1',
metadata: { name: 'file1_1' }, stream_count: 4), # metadata: { name: 'file1_1' }, stream_count: 4),
helper.entry('/folder1/f1_2', # helper.entry('/folder1/f1_2',
metadata: { name: 'file1_2' }, stream_count: 5), # metadata: { name: 'file1_2' }, stream_count: 5),
helper.entry('/folder2/f2_1', metadata: { name: 'file2_1' }), # helper.entry('/folder2/f2_1', metadata: { name: 'file2_1' }),
helper.entry('/folder2/f2_2', metadata: { name: 'file2_2' }) # helper.entry('/folder2/f2_2', metadata: { name: 'file2_2' })
] # ]
RSpec.describe 'parse and display a database' do RSpec.describe 'parse and display a database' do
def update_with_schema(schema) def update_with_schema(schema)
......
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Nilm' do
describe 'object' do
let(:nilm) { Nilm.new }
specify { expect(nilm).to respond_to(:name) }
specify { expect(nilm).to respond_to(:description) }
specify { expect(nilm).to respond_to(:url) }
specify { expect(nilm).to respond_to(:db) }
end
end
# frozen_string_literal: true
require 'rails_helper'
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)
# run the NILM creation
nilm_creator = CreateNilm.new
nilm_creator.build(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
# ...and the database has been populated
expect(builder).to have_received(:update_db)
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