Commit 75bcc2b7 by John

working on dbadapter

parent a214d250
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
// Place all the styles related to the db_files controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
// Place all the styles related to the DbStreams controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
# frozen_string_literal: true
# Handles construction of database objects
class DbBuilder
def initialize(db:, db_service:)
@db = db
@db_service = db_service
end
def update_db
entries = @db_service.schema
#split path into chunks
entries.map! do |entry|
entry[:chunks] = entry[:path][1..-1].split('/').reverse
entry
end
if(@db.root_folder == nil)
@db.root_folder=__build_folder(entries: entries, default_name: 'root')
end
#group entries by first chunk
#find or create an entry with the single chunk 'info'
#if the group contains one entry that is *not* 'info', make it a file
#if the group contains multiple entries, make it a folder
#..recursive
end
protected
def __build_folder(entries:, default_name:)
folder = DbFolder.new(name: default_name)
entry_groups = Hash.new()
entries.map do |entry|
__add_to_group(entry_groups, entry[:chunks].pop, entry)
end
entry_groups.each do |name, entry_group|
if(entry_group.length==1)
folder.db_files << __build_file(entry: entry_group, default_name: name)
elsif(entry_group.length > 1)
folder.subfolders << __build_folder(entries: entry_group, default_name: name)
end
end
folder
end
def __add_to_group(entry_groups, group_name, entry)
if entry_groups[group_name] == nil
entry_groups[group_name] = [entry]
else
entry_groups[group_name].push(entry)
end
end
def __build_file(entry:, default_name:)
file = DbFile.new(name: default_name)
end
end
# frozen_string_literal: true
# Controller for Database Objects
class DbsController < ApplicationController
end
# frozen_string_literal: true
# Database object
class Db < ActiveRecord::Base
belongs_to :root_folder, foreign_key: 'db_folder_id', class_name: 'DbFolder'
end
......@@ -4,7 +4,7 @@
# and one or more DbFolders as subfolders
class DbFolder < ActiveRecord::Base
belongs_to :parent, class_name: 'DbFolder'
has_many :subfolders
has_many :subfolders, class_name: 'DbFolder', foreign_key: 'db_folder_id'
has_many :db_files
def insert_file(file:)
......
Rails.application.routes.draw do
resources :dbs
resources :db_streams
resources :db_files
resources :db_folders
......
class CreateDbs < ActiveRecord::Migration
def change
create_table :dbs do |t|
t.string :url
t.integer :db_folder_id
t.timestamps null: false
end
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160517015954) do
ActiveRecord::Schema.define(version: 20160520145007) do
create_table "db_files", force: :cascade do |t|
t.string "name"
......@@ -44,4 +44,11 @@ ActiveRecord::Schema.define(version: 20160517015954) do
t.datetime "updated_at", null: false
end
create_table "dbs", force: :cascade do |t|
t.string "url"
t.integer "db_folder_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
# frozen_string_literal: true
require 'rails_helper'
# schema data
def entry(path, type = 'uint8_1')
{ path: path, type: type,
start_time: 0, end_time: 0,
total_rows: 0, total_time: 0
}
end
simple_db = [
entry('/folder1/info'),
entry('/folder1/file1'),
entry('/folder1/file2'),
entry('/empty_folder/info')
]
describe DbBuilder do
describe 'update_db' do
let(:db) { Db.new }
it 'initializes an empty database' do
db_service = instance_double('DbService', schema: simple_db)
db_builder = DbBuilder.new(db: db, db_service: db_service)
db_builder.update_db
expect(db.root_folder.name).to eq('root')
end
end
end
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe DbsController, type: :controller do
end
# frozen_string_literal: true
FactoryGirl.define do
factory :db do
end
end
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'DbStream' do
describe 'object' do
let(:db) { Db.new }
specify { expect(db).to respond_to(:url) }
specify { expect(db).to respond_to(:root_folder) }
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