Commit f56b2319 by John Doe

build file names from metadata

parent 60ff3dd1
......@@ -108,11 +108,9 @@ class DbBuilder
def __process_folder_contents(folder, groups)
groups.each do |name, entry_group|
if entry_group.length == 1
folder.db_files << __build_file(folder: folder, entry: entry_group[0],
default_name: name)
__build_file(folder: folder, entry: entry_group[0], default_name: name)
elsif entry_group.length > 1
folder.subfolders << __parse_folder_entries(parent: folder,
entries: entry_group,
__parse_folder_entries(parent: folder, entries: entry_group,
default_name: name)
end
end
......@@ -122,7 +120,9 @@ class DbBuilder
# specified path.
def __build_file(folder:, entry:, default_name:)
file = folder.db_files.find_by_path(entry[:path])
file ||= DbFile.new(name: default_name)
file ||= DbFile.new(db_folder: folder, path: entry[:path])
info = { name: default_name }.merge(entry[:metadata])
file.update_attributes(info)
file.save!
file
end
......
......@@ -5,30 +5,37 @@ helper = DbSchemaHelper.new
# a simple schema that could be returned
# from DbAdapater.schema
simple_db = [ # folder1
helper.entry('/folder1/file1_1'), # `- file1_1
helper.entry('/folder1/file1_2'), # - file1_2
helper.entry('/folder2/file2_1'), # folder2
helper.entry('/folder2/file2_2'), # '- file2_1
] # `- file2_2
# folder1
# `- file1_1
# - file1_2
# folder2
# '- file2_1
# `- file2_2
simple_db = [
helper.entry('/folder1/f1_1', metadata: { name: 'file1_1' }),
helper.entry('/folder1/f1_2', metadata: { name: 'file1_2' }),
helper.entry('/folder2/f2_1', metadata: { name: 'file2_1' }),
helper.entry('/folder2/f2_2', metadata: { name: 'file2_2' })
]
describe DbBuilder do
describe '*update_db*' do
before(:all) do
def update_with_schema(schema)
@db = Db.new
@db_builder = DbBuilder.new(db: @db)
end
describe 'given the simple_db schema' do
before(:all) do
@db_builder.update_db(schema: simple_db)
@db_builder.update_db(schema: schema)
@root = @db.root_folder
end
describe 'given the simple_db schema' do
it 'builds a root folder' do
update_with_schema(simple_db)
expect(@root.name).to eq('root')
expect(@root.subfolders.count).to eq(2)
expect(@root.db_files.count).to eq(0)
end
it 'builds sub-folder1' do
update_with_schema(simple_db)
folder1 = @root.subfolders[0]
expect(folder1.name).to eq('folder1')
expect(folder1.db_files.count).to eq(2)
......@@ -36,6 +43,7 @@ describe DbBuilder do
expect(folder1.db_files[1].name).to eq('file1_2')
end
it 'builds sub-folder2' do
update_with_schema(simple_db)
folder2 = @root.subfolders[1]
expect(folder2.name).to eq('folder2')
expect(folder2.db_files.count).to eq(2)
......@@ -43,13 +51,18 @@ describe DbBuilder do
expect(folder2.db_files[1].name).to eq('file2_2')
end
end
describe 'given simple_db schema with folder info streams' do
before(:all) do
simple_db << helper.entry('/folder1/info', metadata: { name: 'first' })
simple_db << helper.entry('/folder2/info', metadata: { name: 'second' })
@db_builder.update_db(schema: simple_db)
it 'uses folder info streams if available' do
schema = Array.new(simple_db)
schema << helper.entry('/folder1/info', metadata: { name: 'first' })
update_with_schema(schema)
folder1 = @root.subfolders[0]
expect(folder1.name).to eq('first')
end
it 'uses the name info'
it 'builds file streams' do
schema = Array.new(simple_db)
schema << helper.entry('/folder1/info', metadata: { name: 'first' })
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