Commit 9ae9c64b by John

refactored, still have long UpdateDb class

parent 80f7dbf8
Showing with 41 additions and 25 deletions
...@@ -119,31 +119,32 @@ class UpdateDb ...@@ -119,31 +119,32 @@ class UpdateDb
# convert the groups into subfolders and files # convert the groups into subfolders and files
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 all paths in the entry group are the same up to a ~decim # check if this entry group is a file
# then this is a file if __file_entry_group? entry_group
base_paths = entry_group.map do |entry|
entry[:path].gsub(decimation_tag, '')
end
if base_paths.uniq.count == 1
__build_file(folder: folder, entries: entry_group, default_name: name) __build_file(folder: folder, entries: entry_group, default_name: name)
# otherwise this is a subfolder # otherwise this is a subfolder
elsif entry_group.length > 1 else
__parse_folder_entries(parent: folder, entries: entry_group, __parse_folder_entries(parent: folder, entries: entry_group,
default_name: name) default_name: name)
end end
end end
end end
# return true if this entry_group corresponds to a single file
def __file_entry_group?(entry_group)
# strip decim tag to form base paths
base_paths = entry_group.map do |entry|
entry[:path].gsub(decimation_tag, '')
end
# there is only one base path (this is a file)
base_paths.uniq.count == 1
end
# create or update a DbFile object at the # create or update a DbFile object at the
# specified path. # specified path.
def __build_file(folder:, entries:, default_name:) def __build_file(folder:, entries:, default_name:)
# find the base file entry base_entry = __get_base_entry(entries)
base_entry = entries.select { |entry| !entry[:path].match(decimation_tag) } return unless base_entry # corrupt file, don't process
unless base_entry.count == 1
warnings << "Missing base stream for #{default_name} in #{folder.name}"
return
end
base_entry = base_entry.first
# find or create the DbFile object # find or create the DbFile object
file = folder.db_files.find_by_path(base_entry[:path]) file = folder.db_files.find_by_path(base_entry[:path])
file ||= DbFile.new(db_folder: folder, path: base_entry[:path]) file ||= DbFile.new(db_folder: folder, path: base_entry[:path])
...@@ -152,20 +153,35 @@ class UpdateDb ...@@ -152,20 +153,35 @@ class UpdateDb
file.update_attributes(info.merge(base_entry[:attributes])) file.update_attributes(info.merge(base_entry[:attributes]))
file.save! file.save!
# add the decimations # add the decimations
decim_entries = entries.select do |entry| __process_decimations(file: file, entries: entries)
entry[:path].match(decimation_tag) end
# return the base file entry in an entry group
# adds a warning and returns nil if base entry is missing
def __get_base_entry(entries)
# find the base file entry
base_entry = entries.select { |entry| !entry[:path].match(decimation_tag) }
unless base_entry.count == 1
warnings << "Missing base stream for #{default_name} in #{folder.name}"
return nil
end end
__build_decimations(file: file, entries: decim_entries) base_entry.first
end end
# create or update DbDecimation objects for a DbFile # go through all decimation entries and build a DbDecimation
def __build_decimations(file:, entries:) def __process_decimations(file:, entries:)
entries.each do |entry| decim_entries = entries.select { |e| e[:path].match(decimation_tag) }
level = entry[:path].match(decimation_tag)[1].to_i decim_entries.each do |entry|
decim = file.db_decimations.find_by_level(level) __build_decimation(file: file, entry: entry)
decim ||= DbDecimation.new(db_file: file, level: level)
decim.update_attributes(entry[:attributes])
decim.save!
end end
end end
# create or update DbDecimation object
def __build_decimation(file:, entry:)
level = entry[:path].match(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])
decim.save!
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