Class: DPN::Bagit::SerializedBag
- Inherits:
-
Object
- Object
- DPN::Bagit::SerializedBag
- 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
-
#fixity(algorithm) ⇒ String
Returns the fixity of the serialized version of the bag.
-
#initialize(_location) ⇒ SerializedBag
constructor
Create a SerializedBag.
-
#location ⇒ String
Returns the local file location of the Bag.
-
#size ⇒ Fixnum
Returns the size of the serialized bag.
-
#unserialize! ⇒ Bag
Unserialize the bag into the local filesystem.
Constructor Details
#initialize(_location) ⇒ SerializedBag
Create a SerializedBag
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.
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 |
#location ⇒ String
Returns the local file location of the Bag.
30 31 32 |
# File 'lib/dpn/bagit/serialized_bag.rb', line 30 def location() return @location end |
#size ⇒ Fixnum
Returns the size of the serialized bag.
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.
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 |