Commit f56b2319 by John Doe

build file names from metadata

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