Class: BagIt::Bag
- Inherits:
-
Object
- Object
- BagIt::Bag
- Defined in:
- lib/bagit/bag.rb,
lib/bagit/valid.rb
Overview
Represents the state of a bag on a filesystem
Instance Attribute Summary collapse
-
#bag_dir ⇒ Object
readonly
Returns the value of attribute bag_dir.
Instance Method Summary collapse
-
#add_file(relative_path, src_path = nil) ⇒ Object
Add a bag file at the given path relative to data_dir.
-
#bag_files ⇒ Object
Return the paths to each bag file relative to bag_dir.
-
#data_dir ⇒ Object
Return the path to the data directory.
-
#empty? ⇒ Boolean
Test if this bag is empty (no files).
-
#gc! ⇒ Object
Remove all empty directory trees from the bag.
-
#get(relative_path) ⇒ Object
Retrieve the IO handle for a file in the bag at a given path relative to data_dir.
-
#initialize(path, info = {}, _create = false) ⇒ Bag
constructor
Make a new Bag based at path.
-
#paths ⇒ Object
Get all bag file paths relative to the data dir.
-
#payload_oxum ⇒ Object
Get the Oxum for the payload files.
-
#remove_file(relative_path) ⇒ Object
Remove a bag file at the given path relative to data_dir.
-
#tag_files ⇒ Object
Return the paths to each tag file relative to bag_dir.
Methods included from Fetch
#add_remote_file, #fetch!, #fetch_txt_file, #move_current_fetch_txt, #rename_old_fetch_txt
Methods included from Manifest
#add_tag_file, #delete_tag_file, #encode_filename, #fixed?, #manifest!, #manifest_file, #manifest_files, #remove_tag_file, #tagmanifest!, #tagmanifest_file, #tagmanifest_files, #write_checksum, #write_md5, #write_sha1, #write_sha256, #write_sha512
Methods included from Info
#bag_info, #bag_info_txt_file, #bagit, #bagit_txt_file, #update_bag_date, #write_bag_info, #write_bagit
Methods included from Validity
#complete?, #consistent?, #decode_filename, #manifest_type, #valid_oxum?
Constructor Details
#initialize(path, info = {}, _create = false) ⇒ Bag
Make a new Bag based at path
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bagit/bag.rb', line 21 def initialize(path, info = {}, _create = false) @bag_dir = path # make the dir structure if it doesn't exist FileUtils.mkdir bag_dir unless File.directory? bag_dir FileUtils.mkdir data_dir unless File.directory? data_dir # write some tag info if its not there write_bagit("BagIt-Version" => SPEC_VERSION, "Tag-File-Character-Encoding" => "UTF-8") unless File.exist? bagit_txt_file write_bag_info(info) unless File.exist? bag_info_txt_file end |
Instance Attribute Details
#bag_dir ⇒ Object (readonly)
Returns the value of attribute bag_dir.
13 14 15 |
# File 'lib/bagit/bag.rb', line 13 def bag_dir @bag_dir end |
Instance Method Details
#add_file(relative_path, src_path = nil) ⇒ Object
Add a bag file at the given path relative to data_dir
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/bagit/bag.rb', line 55 def add_file(relative_path, src_path = nil) path = File.join(data_dir, relative_path) raise "Bag file exists: #{relative_path}" if File.exist? path FileUtils.mkdir_p File.dirname(path) f = if src_path.nil? File.open(path, "w") { |io| yield io } else FileUtils.cp src_path, path end write_bag_info f end |
#bag_files ⇒ Object
Return the paths to each bag file relative to bag_dir
39 40 41 |
# File 'lib/bagit/bag.rb', line 39 def bag_files Dir[File.join(data_dir, "**", "*")].select { |f| File.file? f } end |
#data_dir ⇒ Object
Return the path to the data directory
34 35 36 |
# File 'lib/bagit/bag.rb', line 34 def data_dir File.join @bag_dir, "data" end |
#empty? ⇒ Boolean
Test if this bag is empty (no files)
85 86 87 |
# File 'lib/bagit/bag.rb', line 85 def empty? bag_files.empty? end |
#gc! ⇒ Object
Remove all empty directory trees from the bag
105 106 107 108 109 110 111 112 |
# File 'lib/bagit/bag.rb', line 105 def gc! Dir.entries(data_dir).each do |f| unless %w[.. .].include? f abs_path = File.join data_dir, f File.clean abs_path end end end |
#get(relative_path) ⇒ Object
Retrieve the IO handle for a file in the bag at a given path relative to data_dir
78 79 80 81 82 |
# File 'lib/bagit/bag.rb', line 78 def get(relative_path) path = File.join(data_dir, relative_path) return nil unless File.exist?(path) File.open(path) end |
#paths ⇒ Object
Get all bag file paths relative to the data dir
90 91 92 |
# File 'lib/bagit/bag.rb', line 90 def paths bag_files.collect { |f| f.sub(data_dir + "/", "") } end |
#payload_oxum ⇒ Object
Get the Oxum for the payload files
95 96 97 98 99 100 101 102 |
# File 'lib/bagit/bag.rb', line 95 def payload_oxum bytes = 0 bag_files.each do |f| # TODO: filesystem quirks? Are we getting the stream size or the size on disk? bytes += File.size(f) end bytes.to_s + "." + bag_files.count.to_s end |
#remove_file(relative_path) ⇒ Object
Remove a bag file at the given path relative to data_dir
70 71 72 73 74 |
# File 'lib/bagit/bag.rb', line 70 def remove_file(relative_path) path = File.join(data_dir, relative_path) raise "Bag file does not exist: #{relative_path}" unless File.exist? path FileUtils.rm path end |
#tag_files ⇒ Object
Return the paths to each tag file relative to bag_dir
44 45 46 47 48 49 50 51 52 |
# File 'lib/bagit/bag.rb', line 44 def tag_files files = [] if tagmanifest_files != [] File.open(tagmanifest_files.first) do |f| f.each_line { |line| files << File.join(@bag_dir, line.split(" ")[1]) } end end files end |