Commit 0148696b by John Doe

finished refactor into separate services

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