Sha256: a52decef3b2cd8aed0c504f48c083d19bc0024795c4365d6311819090b6c0cdd

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

module ROCrate
  ##
  # A class to handle writing of RO-Crates to Zip files or directories.
  class Writer
    ##
    # Initialize a new Writer for the given Crate.
    # @param crate [Crate] The RO-Crate to be written.
    def initialize(crate)
      @crate = crate
    end

    ##
    # Write the crate to a directory.
    #
    # @param dir [String] A path for the directory for the crate to be written to. All parent directories will be created.
    # @param overwrite [Boolean] Whether or not to overwrite existing files.
    def write(dir, overwrite: true)
      FileUtils.mkdir_p(dir) # Make any parent directories
      @crate.payload.each do |path, entry|
        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 the crate to a zip file.
    #
    # @param destination [String, ::File] The destination where to write the RO-Crate zip.
    def write_zip(destination)
      Zip::File.open(destination, Zip::File::CREATE) do |zip|
        @crate.payload.each do |path, entry|
          next if entry.directory?
          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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ro-crate-0.5.2 lib/ro_crate/writer.rb
ro-crate-0.5.1 lib/ro_crate/writer.rb
ro-crate-0.5.0 lib/ro_crate/writer.rb
ro-crate-0.4.17 lib/ro_crate/writer.rb