Commit 0148696b by John Doe

finished refactor into separate services

parent 57fe5708
......@@ -19,7 +19,7 @@ class UpdateDb
entries = __create_entries(db_adapter.schema)
updater = UpdateFolder.new(@root_folder, entries)
updater.run()
updater.run
@errors << updater.errors
@warnings << updater.warnings
......@@ -27,9 +27,7 @@ class UpdateDb
# Note: @root_folder gets linked in on
# the first call to __build_folder
# Don't save the result if there were errors
if !@errors.empty?
return false
end
return false unless @errors.empty?
@db.save
end
......@@ -48,5 +46,4 @@ class UpdateDb
entry
end
end
end
......@@ -12,13 +12,18 @@ class UpdateFile
@errors = []
end
def run()
return __update_file(@file, @base_entry, @decimation_entries)
def run
__update_file(@file, @base_entry, @decimation_entries)
end
# regex matching the ~decimXX ending on a stream path
def self.decimation_tag
/~decim-([\d]+)$/
end
# create or update a DbFile object at the
# specified path.
def __update_file(file, base_entry, decimation_entries:)
def __update_file(file, base_entry, decimation_entries)
file.update_attributes(base_entry[:attributes])
file.save!
__build_decimations(file: file,
......@@ -26,12 +31,11 @@ class UpdateFile
__build_streams(file: file, stream_data: base_entry[:streams])
end
# create or update DbDecimations for the
# specified DbFile
def __build_decimations(file:, entry_group:)
entry_group.each do |entry|
level = entry[:path].match(decimation_tag)[1].to_i
level = entry[:path].match(UpdateFile.decimation_tag)[1].to_i
decim = file.db_decimations.find_by_level(level)
decim ||= DbDecimation.new(db_file: file, level: level)
decim.update_attributes(entry[:attributes])
......@@ -50,4 +54,4 @@ class UpdateFile
stream.save!
end
end
end
\ No newline at end of file
end
# frozen_string_literal: true
# Agent class for DbFolders
class DbFolderAgent
class InsertFile
attr_accessor :error_msg
def initialize(db_service:, db_builder:)
......
......@@ -4,7 +4,6 @@
class UpdateFolder
attr_accessor :warnings, :errors
def initialize(folder, entries)
@folder = folder
@entries = entries
......@@ -13,7 +12,7 @@ class UpdateFolder
end
# returns the updated DbFolder object
def run()
def run
# update the folder attributes from metadata
info = __read_info_entry(@entries) || {}
@folder.update_attributes(
......@@ -51,8 +50,6 @@ class UpdateFolder
folder
end
# collect the folder's entries into a set of groups
# based off the next item in their :chunk array
# returns entry_groups which is a Hash with
......@@ -62,17 +59,12 @@ class UpdateFolder
entry_groups = {}
entries.map do |entry|
# group streams by their base paths (ignore ~decim endings)
group_name = entry[:chunks].pop.gsub(decimation_tag, '')
group_name = entry[:chunks].pop.gsub(UpdateFile.decimation_tag, '')
__add_to_group(entry_groups, group_name, entry)
end
entry_groups
end
# regex matching the ~decimXX ending on a stream path
def decimation_tag
/~decim-([\d]+)$/
end
# helper function to __group_entries that handles
# sorting entries into the entry_groups Hash
def __add_to_group(entry_groups, group_name, entry)
......@@ -88,14 +80,12 @@ class UpdateFolder
def __process_folder_contents(folder, groups)
groups.each do |name, entry_group|
if file?(entry_group)
file = __build_file(folder, entry_group, name)
base = __base_entry(entry_group)
updater = UpdateFile.new(file, base, entry_group-[base])
updater = __build_file(folder, entry_group, name)
next if updater.nil? # ignore orphaned decimations
else # its a folder
subfolder = __build_folder(folder, entry_group, name)
updater = UpdateFolder.new(subfolder, entry_group)
updater = __build_folder(folder, entry_group, name)
end
updater.run()
updater.run
@warnings << updater.warnings
@errors << updater.errors
end
......@@ -120,8 +110,10 @@ class UpdateFolder
end
# find or create the file
file = folder.db_files.find_by_path(base[:path])
file ||= DbFile.new(db_folder: folder, path: base[:path], name: default_name)
file
file ||= DbFile.new(db_folder: folder,
path: base[:path], name: default_name)
UpdateFile.new(file, base, entry_group - [base])
end
# find the base stream in this entry_group
......@@ -129,7 +121,7 @@ class UpdateFolder
# adds a warning and returns nil if base entry is missing
def __base_entry(entry_group)
base_entry = entry_group.select { |entry|
entry[:path].match(decimation_tag).nil?
entry[:path].match(UpdateFile.decimation_tag).nil?
}.first
return nil unless base_entry
base_entry
......@@ -141,7 +133,7 @@ class UpdateFolder
path = __build_path(entries)
folder = parent.subfolders.find_by_path(path)
folder ||= DbFolder.new(parent: parent, path: path, name: default_name)
folder
UpdateFolder.new(folder, entries)
end
# all entries agree on a common path
......@@ -153,6 +145,4 @@ class UpdateFolder
parts.pop(entries[0][:chunks].length)
parts.join('/') # stitch parts together to form a path
end
end
\ No newline at end of file
end
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'DbFolderAgent' do
RSpec.describe 'InsertFile' do
describe 'insert_file' do
# mock the DbService and DbBuilder
let(:db_service) { double(create_file: true) }
......@@ -12,9 +12,9 @@ RSpec.describe 'DbFolderAgent' do
let(:parent_folder) { FactoryGirl.build_stubbed(:db_folder) }
it 'adds the given file to the folder' do
db_folder_agent = DbFolderAgent.new(db_service: db_service,
db_builder: db_builder)
db_folder_agent.insert_file(folder: parent_folder, file: new_file)
file_inserter = InsertFile.new(db_service: db_service,
db_builder: db_builder)
file_inserter.insert_file(folder: parent_folder, file: new_file)
expect(new_file.db_folder).to eq(parent_folder)
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