lib/bagit/info.rb in bagit-0.3.1 vs lib/bagit/info.rb in bagit-0.3.2.pre
- old
+ new
@@ -2,11 +2,11 @@
module BagIt
module Info
- @@bag_info_headers = {
+ @@bag_info_headers = {
:agent => 'Bag-Software-Agent',
:org => 'Source-Organization',
:org_addr => 'Organization-Address',
:contact_name => 'Contact-Name',
:contact_phone => 'Contact-Phone',
@@ -33,11 +33,11 @@
{}
end
end
def write_bag_info(hash={})
- hash = bag_info.merge(hash)
+ hash = bag_info.merge(hash)
hash[@@bag_info_headers[:agent]] = "BagIt Ruby Gem (http://bagit.rubyforge.org)" if hash[@@bag_info_headers[:agent]].nil?
hash[@@bag_info_headers[:date]] = Date.today.strftime('%Y-%m-%d') if hash[@@bag_info_headers[:date]].nil?
hash[@@bag_info_headers[:oxum]] = payload_oxum
write_info_file bag_info_txt_file, hash
end
@@ -47,11 +47,11 @@
end
def bagit
read_info_file bagit_txt_file
end
-
+
def write_bagit(hash)
write_info_file bagit_txt_file, hash
end
def update_bag_date
@@ -60,48 +60,48 @@
end
protected
def read_info_file(file)
-
- open(file) do |io|
-
+
+ File.open(file) do |io|
+
entries = io.read.split /\n(?=[^\s])/
-
+
entries.inject({}) do |hash, line|
name, value = line.chomp.split /\s*:\s*/, 2
hash.merge({name => value})
end
-
+
end
-
+
end
def write_info_file(file, hash)
dups = hash.keys.inject(Set.new) do |acc, key|
a = hash.keys.grep(/#{key}/i)
acc + (a.size > 1 ? a : [])
end
-
+
raise "Multiple labels (#{dups.to_a.join ', '}) in #{file}" unless dups.empty?
-
- open(file, 'w') do |io|
-
+
+ File.open(file, 'w') do |io|
+
hash.each do |name, value|
simple_entry = "#{name}: #{value.gsub /\s+/, ' '}"
-
+
entry = if simple_entry.length > 79
simple_entry.wrap(77).indent(2)
else
simple_entry
end
-
+
io.puts entry
end
-
+
end
-
+
end
end
end