Class: BagIt::Bag

Inherits:
Object
  • Object
show all
Includes:
Fetch, Info, Manifest, Validity, Validatable
Defined in:
lib/bagit/bag.rb,
lib/bagit/valid.rb

Overview

Represents the state of a bag on a filesystem

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fetch

#add_remote_file, #fetch!, #fetch_txt_file

Methods included from Manifest

#add_tag_file, #delete_tag_file, #fixed?, #manifest!, #manifest_file, #manifest_files, #remove_tag_file, #tagmanifest!, #tagmanifest_file, #tagmanifest_files

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?, #valid_oxum?

Constructor Details

#initialize(path, info = {}) ⇒ Bag

Make a new Bag based at path



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bagit/bag.rb', line 20

def initialize(path, info={})
  @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
  unless File.exist? bagit_txt_file
    write_bagit("BagIt-Version" => SPEC_VERSION, "Tag-File-Character-Encoding" => "UTF-8")
  end

  unless File.exist? bag_info_txt_file
    write_bag_info(info)
  end
end

Instance Attribute Details

#bag_dirObject (readonly)

Returns the value of attribute bag_dir.



12
13
14
# File 'lib/bagit/bag.rb', line 12

def bag_dir
  @bag_dir
end

Instance Method Details

#add_file(base_path, src_path = nil) ⇒ Object

Add a bag file



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bagit/bag.rb', line 59

def add_file(base_path, src_path=nil)
  path = File.join(data_dir, base_path)
  raise "Bag file exists: #{base_path}" if File.exist? path
  FileUtils::mkdir_p File.dirname(path)

  if src_path.nil?
    f = File.open(path, 'w') { |io| yield io }
  else
    f = FileUtils::cp src_path, path
  end
  write_bag_info
  return f
end

#bag_filesObject

Return the paths to each bag file relative to bag_dir



43
44
45
# File 'lib/bagit/bag.rb', line 43

def bag_files
  Dir[File.join(data_dir, '**', '*')].select { |f| File.file? f }
end

#data_dirObject

Return the path to the data directory



38
39
40
# File 'lib/bagit/bag.rb', line 38

def data_dir
  File.join @bag_dir, 'data'
end

#empty?Boolean

Test if this bag is empty (no files)

Returns:

  • (Boolean)


88
89
90
# File 'lib/bagit/bag.rb', line 88

def empty?
  self.bag_files.empty?
end

#gc!Object

Remove all empty directory trees from the bag



108
109
110
111
112
113
114
115
# File 'lib/bagit/bag.rb', line 108

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(base_path) ⇒ Object

Retrieve the IO handle for a file in the bag



81
82
83
84
85
# File 'lib/bagit/bag.rb', line 81

def get(base_path)
  path = File.join(data_dir, base_path)
  return nil unless File.exist?(path)
  File.open(path)
end

#pathsObject

Get all bag file paths relative to the data dir



93
94
95
# File 'lib/bagit/bag.rb', line 93

def paths
  self.bag_files.collect { |f| f.sub(data_dir + '/', '') }
end

#payload_oxumObject

Get the Oxum for the payload files



98
99
100
101
102
103
104
105
# File 'lib/bagit/bag.rb', line 98

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
  return bytes.to_s + '.' + bag_files.count.to_s
end

#remove_file(base_path) ⇒ Object

Remove a bag file



74
75
76
77
78
# File 'lib/bagit/bag.rb', line 74

def remove_file(base_path)
  path = File.join(data_dir, base_path)
  raise "Bag file does not exist: #{base_path}" unless File.exist? path
  FileUtils::rm path
end

#tag_filesObject

Return the paths to each tag file relative to bag_dir



48
49
50
51
52
53
54
55
56
# File 'lib/bagit/bag.rb', line 48

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