Class: ROCrate::Writer
- Inherits:
-
Object
- Object
- ROCrate::Writer
- Defined in:
- lib/ro_crate/writer.rb
Overview
A class to handle writing of RO-Crates to Zip files or directories.
Instance Method Summary collapse
-
#initialize(crate) ⇒ Writer
constructor
Initialize a new Writer for the given Crate.
-
#write(dir, overwrite: true, skip_preview: false) ⇒ Object
Write the crate to a directory.
-
#write_zip(destination, skip_preview: false) ⇒ Object
Write the crate to a zip file.
Constructor Details
#initialize(crate) ⇒ Writer
Initialize a new Writer for the given Crate.
8 9 10 |
# File 'lib/ro_crate/writer.rb', line 8 def initialize(crate) @crate = crate end |
Instance Method Details
#write(dir, overwrite: true, skip_preview: false) ⇒ Object
Write the crate to a directory.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ro_crate/writer.rb', line 18 def write(dir, overwrite: true, skip_preview: false) FileUtils.mkdir_p(dir) # Make any parent directories @crate.payload.each do |path, entry| next if skip_preview && entry&.source.is_a?(ROCrate::PreviewGenerator) fullpath = ::File.join(dir, path) next if !overwrite && ::File.exist?(fullpath) next if entry.directory? FileUtils.mkdir_p(::File.dirname(fullpath)) if entry.symlink? ::File.symlink(entry.source.readlink, fullpath) else temp = Tempfile.new('ro-crate-temp') begin entry.write_to(temp) temp.close FileUtils.mv(temp, fullpath) ensure temp.unlink end end end end |
#write_zip(destination, skip_preview: false) ⇒ Object
Write the crate to a zip file.
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ro_crate/writer.rb', line 46 def write_zip(destination, skip_preview: false) Zip::File.open(destination, Zip::File::CREATE) do |zip| @crate.payload.each do |path, entry| next if entry.directory? next if skip_preview && entry&.source.is_a?(ROCrate::PreviewGenerator) if entry.symlink? zip.add(path, entry.path) if entry.path else zip.get_output_stream(path) { |s| entry.write_to(s) } end end end end |