Class: DPN::Bagit::SerializedBag

Inherits:
Object
  • Object
show all
Defined in:
lib/dpn/bagit/serialized_bag.rb

Overview

A wrapper for a serialized Bag-It bag on disk. Once created, will not change with changes made to the underlying filesystem bag; in that case, a new object should be created.

Instance Method Summary collapse

Constructor Details

#initialize(_location) ⇒ SerializedBag

Create a SerializedBag

Parameters:

  • _location (String)

    Path to the file.



11
12
13
14
15
16
17
18
# File 'lib/dpn/bagit/serialized_bag.rb', line 11

def initialize(_location)
  if File.exists?(_location) == false
    raise ArgumentError, "File does not exist!"
  end

  @location = _location
  @cachedFixity = nil
end

Instance Method Details

#fixity(algorithm) ⇒ String

Returns the fixity of the serialized version of the bag.

Parameters:

  • algorithm (Symbol)

    The algorithm to use for calculation.

Returns:

  • (String)

    The fixity of the file.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dpn/bagit/serialized_bag.rb', line 38

def fixity(algorithm)
  if @cachedFixity == nil
    case algorithm
    when :sha256
      digest = Digest::SHA256
    else
      raise ArgumentError, "Unknown algorithm."
    end

    @cachedFixity = digest.file(@location).hexdigest
  end
  return @cachedFixity
end

#locationString

Returns the local file location of the Bag.

Returns:

  • (String)

    The location, which can be relative or absolute.



30
31
32
# File 'lib/dpn/bagit/serialized_bag.rb', line 30

def location()
  return @location
end

#sizeFixnum

Returns the size of the serialized bag.

Returns:

  • (Fixnum)

    Apparent size of the bag in bytes.



23
24
25
# File 'lib/dpn/bagit/serialized_bag.rb', line 23

def size()
  return File.size(@location)
end

#unserialize!Bag

Unserialize the bag into the local filesystem. This object is unchanged. Requires sufficient permissions and disk space.

Returns:

  • (Bag)

    A bag made from the unserialized object.

Raises:

  • (RuntimeError)


56
57
58
59
60
61
# File 'lib/dpn/bagit/serialized_bag.rb', line 56

def unserialize!()
  `/bin/tar -xf #{@location} -C #{File.dirname(@location)}`
  raise RuntimeError, "cannot untar #{file}" unless $?.success?
  name = File.basename(@location).to_s.sub(/\..*/,'')    # remove the file extension
  return DPN::Bagit::Bag.new(File.join(File.dirname(@location), name))
end