Skip to content

Commit

Permalink
Add proper version handling to Pairtree.at()
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Jun 10, 2011
1 parent 9888e0b commit 3b481d9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
32 changes: 30 additions & 2 deletions lib/pairtree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
module Pairtree
class IdentifierError < Exception; end
class PathError < Exception; end
class VersionMismatch < Exception; end

SPEC_VERSION = 0.1

def self.at path, args = {}
args = { :prefix => nil, :version => nil, :create => false }.merge(args)
args[:version] ||= SPEC_VERSION
args[:version] = args[:version].to_f

root_path = File.join(path, 'pairtree_root')
prefix_file = File.join(path, 'pairtree_prefix')

version_file = File.join(path, pairtree_version_filename(args[:version]))
existing_version_file = Dir[File.join(path, "pairtree_version*")].sort.last

if args.delete(:create)
if File.exists?(path) and not File.directory?(path)
Expand All @@ -25,6 +31,18 @@ def self.at path, args = {}
unless File.exists? prefix_file
File.open(prefix_file, 'w') { |f| f.write(args[:prefix].to_s) }
end

if existing_version_file
if existing_version_file != version_file
stored_version = existing_version_file.scan(/([0-9]+)_([0-9]+)/).flatten.join('.').to_f
raise VersionMismatch, "Version #{args[:version]} specified, but #{stored_version} found."
end
else
args[:version] ||= SPEC_VERSION
version_file = File.join(path, pairtree_version_filename(args[:version]))
File.open(version_file, 'w') { |f| f.write %{This directory conforms to Pairtree Version #{args[:version]}. Updated spec: http://www.cdlib.org/inside/diglib/pairtree/pairtreespec.html} }
existing_version_file = version_file
end
else
unless File.directory? root_path
raise PathError, "#{path} does not point to an existing pairtree"
Expand All @@ -36,8 +54,18 @@ def self.at path, args = {}
raise IdentifierError, "Specified prefix #{args[:prefix].inspect} does not match stored prefix #{stored_prefix.inspect}"
end
args[:prefix] = stored_prefix

stored_version = existing_version_file.scan(/([0-9]+)_([0-9]+)/).flatten.join('.').to_f
args[:version] ||= stored_version
unless args[:version] == stored_version
raise VersionMismatch, "Version #{args[:version]} specified, but #{stored_version} found."
end

Pairtree::Root.new(File.join(path, 'pairtree_root'), args)
end


private
def self.pairtree_version_filename(version)
"pairtree_version#{version.to_s.gsub(/\./,'_')}"
end
end
12 changes: 11 additions & 1 deletion spec/pairtree/pairtree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
pt.prefix.should == prefix
File.read(File.join(@base_path, 'pairtree_prefix')).should == prefix
pt.root.should == File.join(@base_path, 'pairtree_root')
pt.pairtree_root.class.should == Dir
pt.pairtree_version.should == Pairtree::SPEC_VERSION
end

end
Expand Down Expand Up @@ -54,6 +54,16 @@
lambda { Pairtree.at(@base_path, :prefix => 'wrong-prefix:') }.should raise_error(Pairtree::IdentifierError)
end

it "should not complain if the given version matches the saved version" do
Pairtree.at(@base_path, :version => Pairtree::SPEC_VERSION).pairtree_version.should == Pairtree::SPEC_VERSION
Pairtree.at(@base_path, :version => Pairtree::SPEC_VERSION, :create => true).pairtree_version.should == Pairtree::SPEC_VERSION
end

it "should raise an error if the given version does not match the saved version" do
lambda { Pairtree.at(@base_path, :version => 0.2) }.should raise_error(Pairtree::VersionMismatch)
lambda { Pairtree.at(@base_path, :version => 0.2, :create => true) }.should raise_error(Pairtree::VersionMismatch)
end

end

end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory conforms to Pairtree Version 0.1. Updated spec: http://www.cdlib.org/inside/diglib/pairtree/pairtreespec.html

0 comments on commit 3b481d9

Please sign in to comment.