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 Attribute Summary collapse
-
#location ⇒ String
readonly
The location, which can be relative or absolute.
Instance Method Summary collapse
-
#fixity(algorithm) ⇒ String
Returns the fixity of the serialized version of the bag.
-
#initialize(path) ⇒ SerializedBag
constructor
Create a SerializedBag.
-
#name ⇒ String
Returns the file name for the serialized bag, without it’s extension.
-
#path ⇒ String
Returns the directory path to the serialized bag.
-
#size ⇒ Fixnum
Returns the size of the serialized bag (in bytes).
-
#unserialize! ⇒ DPN::Bagit::Bag
Unserialize the bag into the local filesystem.
Constructor Details
#initialize(path) ⇒ SerializedBag
Create a SerializedBag
15 16 17 18 |
# File 'lib/dpn/bagit/serialized_bag.rb', line 15 def initialize(path) raise ArgumentError, "File does not exist!" unless File.exist?(path) @location = path end |
Instance Attribute Details
#location ⇒ String (readonly)
Returns The location, which can be relative or absolute.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dpn/bagit/serialized_bag.rb', line 9 class DPN::Bagit::SerializedBag attr_reader :location # Create a SerializedBag # @param path [String] Path to the file. def initialize(path) raise ArgumentError, "File does not exist!" unless File.exist?(path) @location = path end # Returns the file name for the serialized bag, without it's extension. # @return [String] name def name @name ||= File.basename(location, File.extname(location)) end # Returns the directory path to the serialized bag. # @return [String] path def path @path ||= File.dirname(location) end # Returns the size of the serialized bag (in bytes). # @return [Fixnum] size def size File.size(location) end # Returns the fixity of the serialized version of the bag. # @param algorithm [Symbol] The algorithm to use for calculation. # @return [String] fixity def fixity(algorithm) @cachedFixity ||= begin case algorithm when :sha256 digest = Digest::SHA256 else raise ArgumentError, "Unknown algorithm." end digest.file(location).hexdigest end end # Unserialize the bag into the local filesystem. This object # is unchanged. Requires sufficient permissions and disk space. # @return [DPN::Bagit::Bag] A bag made from the unserialized object. def unserialize! `/bin/tar -xf #{location} -C #{path} 2> /dev/null` raise RuntimeError, "cannot untar #{location}" unless $?.success? DPN::Bagit::Bag.new(File.join(path, name)) end end |
Instance Method Details
#fixity(algorithm) ⇒ String
Returns the fixity of the serialized version of the bag.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dpn/bagit/serialized_bag.rb', line 41 def fixity(algorithm) @cachedFixity ||= begin case algorithm when :sha256 digest = Digest::SHA256 else raise ArgumentError, "Unknown algorithm." end digest.file(location).hexdigest end end |
#name ⇒ String
Returns the file name for the serialized bag, without it’s extension.
22 23 24 |
# File 'lib/dpn/bagit/serialized_bag.rb', line 22 def name @name ||= File.basename(location, File.extname(location)) end |
#path ⇒ String
Returns the directory path to the serialized bag.
28 29 30 |
# File 'lib/dpn/bagit/serialized_bag.rb', line 28 def path @path ||= File.dirname(location) end |
#size ⇒ Fixnum
Returns the size of the serialized bag (in bytes).
34 35 36 |
# File 'lib/dpn/bagit/serialized_bag.rb', line 34 def size File.size(location) end |
#unserialize! ⇒ DPN::Bagit::Bag
Unserialize the bag into the local filesystem. This object is unchanged. Requires sufficient permissions and disk space.
56 57 58 59 60 |
# File 'lib/dpn/bagit/serialized_bag.rb', line 56 def unserialize! `/bin/tar -xf #{location} -C #{path} 2> /dev/null` raise RuntimeError, "cannot untar #{location}" unless $?.success? DPN::Bagit::Bag.new(File.join(path, name)) end |