-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from lyrasis/v4-0-0
Release v4.0.0
- Loading branch information
Showing
77 changed files
with
1,170 additions
and
577 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module CollectionspaceMigrationTools | ||
# Namespace for the batch archive CSV, if enabled in client config. | ||
# | ||
# The archive CSV contains data from the batches CSV for batches deleted after | ||
# completed ingest (with or without errors). Rows for batches deleted at an | ||
# earlier workflow stage are not added to the archive CSV. | ||
module ArchiveCsv | ||
extend Dry::Monads[:result, :do] | ||
|
||
module_function | ||
|
||
# @return [String] | ||
def path | ||
File.join(CMT.config.client.base_dir, | ||
CMT.config.client.batch_archive_filename) | ||
end | ||
|
||
# @return [Boolean] | ||
def present? = File.exist?(path) | ||
|
||
def file_check | ||
case present? | ||
when true | ||
Success() | ||
when false | ||
Failure(file_check_failure_msg) | ||
end | ||
end | ||
|
||
def parse | ||
data = File.read(path) | ||
table = CSV.parse(data, headers: true) | ||
rescue => err | ||
msg = "#{err.message} IN #{err.backtrace[0]}" | ||
Failure(CMT::Failure.new(context: "#{self.class.name}.#{__callee__}", | ||
message: msg)) | ||
else | ||
Success(table) | ||
end | ||
|
||
def file_check_failure_msg = "No archives CSV file present" | ||
end | ||
end |
54 changes: 54 additions & 0 deletions
54
lib/collectionspace_migration_tools/archive_csv/archiver.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
require "csv" | ||
|
||
module CollectionspaceMigrationTools | ||
module ArchiveCsv | ||
class Archiver | ||
include Dry::Monads[:result] | ||
include Dry::Monads::Do.for(:call) | ||
|
||
class << self | ||
def call(...) | ||
new.call(...) | ||
end | ||
end | ||
|
||
def initialize( | ||
path: CMT::ArchiveCsv.path, | ||
headers: CMT::Batch::Csv::Headers.all_headers | ||
) | ||
@path = path | ||
@headers = headers | ||
end | ||
|
||
# @param batch[CMT::Batch::Batch] | ||
def call(batch) | ||
_present = if CMT::ArchiveCsv.present? | ||
yield CMT::ArchiveCsv.file_check | ||
else | ||
yield CMT::ArchiveCsv::Creator.call | ||
end | ||
_write = yield write_row(batch) | ||
|
||
Success(batch) | ||
end | ||
|
||
private | ||
|
||
attr_reader :path, :headers | ||
|
||
def write_row(batch) | ||
CSV.open(path, "a", headers: true) do |csv| | ||
csv << headers.map { |hdr| batch.send(hdr.to_sym) } | ||
end | ||
rescue => err | ||
msg = "#{err.message} IN #{err.backtrace[0]}" | ||
Failure(CMT::Failure.new(context: "#{self.class.name}.#{__callee__}", | ||
message: msg)) | ||
else | ||
Success() | ||
end | ||
end | ||
end | ||
end |
51 changes: 51 additions & 0 deletions
51
lib/collectionspace_migration_tools/archive_csv/checker.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require "csv" | ||
|
||
module CollectionspaceMigrationTools | ||
module ArchiveCsv | ||
class Checker | ||
include Dry::Monads[:result] | ||
include Dry::Monads::Do.for(:call) | ||
|
||
class << self | ||
def call(...) | ||
new(...).call | ||
end | ||
end | ||
|
||
# @param path [String] | ||
# @param headers [Array<String>] | ||
def initialize( | ||
path: CMT::ArchiveCsv.path, | ||
headers: CMT::Batch::Csv::Headers.all_headers | ||
) | ||
@path = path | ||
@headers = headers | ||
end | ||
|
||
def call | ||
_exist = yield CMT::ArchiveCsv.file_check | ||
table = yield CMT::ArchiveCsv.parse | ||
_hdrs = yield header_check(table) | ||
|
||
Success(table) | ||
end | ||
|
||
private | ||
|
||
attr_reader :path, :headers | ||
|
||
def header_check(table) | ||
return Success() if table.headers == headers | ||
|
||
Failure(header_check_failure_msg) | ||
end | ||
|
||
def header_check_failure_msg | ||
"Archive CSV headers are not up-to-date, so archiving may "\ | ||
"fail unexpectedly. Run `thor archive:fix_csv` to fix" | ||
end | ||
end | ||
end | ||
end |
48 changes: 48 additions & 0 deletions
48
lib/collectionspace_migration_tools/archive_csv/creator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require "csv" | ||
|
||
module CollectionspaceMigrationTools | ||
module ArchiveCsv | ||
# Creates new batches archive CSV | ||
class Creator | ||
include CMT::Batch::Csv::Headers | ||
include Dry::Monads[:result] | ||
|
||
class << self | ||
def call | ||
new.call | ||
end | ||
end | ||
|
||
def initialize | ||
@path = CMT::ArchiveCsv.path | ||
@headers = all_headers | ||
end | ||
|
||
def call | ||
if File.exist?(path) | ||
puts "#{path} already exists; leaving it alone" | ||
|
||
Success() | ||
else | ||
build_archive_csv | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :path, :headers | ||
|
||
def build_archive_csv | ||
CSV.open(path, "wb") { |csv| csv << headers } | ||
rescue => err | ||
msg = "#{err.message} IN #{err.backtrace[0]}" | ||
Failure(CMT::Failure.new(context: "#{self.class.name}.#{__callee__}", | ||
message: msg)) | ||
else | ||
File.exist?(path) ? Success() : Failure() | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.