Skip to content

Commit 7021c21

Browse files
committed
Ensure prefix is set correctly
* Always pass prefix argument to pairtree * Defer to underlying creation in pairtree gem
1 parent 56c5d05 commit 7021c21

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
44

55
# Specify your gem's dependencies in ht-pairtree.gemspec
66
gemspec
7+
8+
group :development do
9+
gem "pry"
10+
end

lib/ht/pairtree.rb

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,42 +38,31 @@ def path_for(htid)
3838

3939
# Get the underlying pairtree for the given obj.
4040
# @param [String] htid The HathiTrust ID for an object
41+
# @param [Boolean] create Whether to create the pairtree if it doesn't exist
4142
# @return [Pairtree] the pairtree for that object
42-
def pairtree_for(htid)
43-
pairtree_root(htid)
43+
def pairtree_for(htid, create: false)
44+
pairtree_root(htid, create: create)
4445
end
4546

46-
# Create a pairtree for the given htid. Allow namespace creation
47-
# only if told to.
47+
# Create the pairtree directory for the given htid. Allow namespace
48+
# creation only if told to.
4849
# @param htid [String] The HTID
4950
# @param new_namespace_allowed [Boolean] Whether or not to error if the namespace DNE
5051
# @raise [NamespaceDoesNotExist] if namespace DNE and new namespace not allowed
5152
# @return [Pairtree::Obj] the underlying pairtree object
5253
def create(htid, new_namespace_allowed: false)
53-
if !namespace_exists?(htid)
54-
if new_namespace_allowed
55-
create_namespace_dir(htid)
56-
else
57-
raise NamespaceDoesNotExist.new("Namespace #{namespace(htid)} does not exist")
58-
end
54+
unless namespace_exists?(htid) || new_namespace_allowed
55+
raise NamespaceDoesNotExist.new("Namespace #{namespace(htid)} does not exist")
5956
end
60-
pairtree_for(htid).mk(htid)
57+
pairtree_for(htid, create: new_namespace_allowed).mk(htid)
6158
end
6259

60+
private
61+
6362
def namespace_exists?(htid)
6463
namespace_dir(htid).exist?
6564
end
6665

67-
def create_namespace_dir(htid)
68-
ndir = namespace_dir(htid)
69-
return self if Dir.exist?(ndir)
70-
Dir.mkdir(ndir)
71-
File.open(ndir + "pairtree_prefix", "w:utf-8") { |f| f.print namespace(htid) }
72-
File.open(ndir + "pairtree_version0_1", "w:utf-8") { |f| }
73-
Dir.mkdir(ndir + "pairtree_root")
74-
self
75-
end
76-
7766
def namespace_dir(htid)
7867
@root + namespace(htid)
7968
end
@@ -82,8 +71,12 @@ def namespace(htid)
8271
htid.split(".", 2).first
8372
end
8473

85-
def pairtree_root(htid)
86-
::Pairtree.at(namespace_dir(htid))
74+
def pairtree_root(htid, create: false)
75+
::Pairtree.at(namespace_dir(htid), prefix: pairtree_prefix(htid), create: create )
76+
end
77+
78+
def pairtree_prefix(htid)
79+
namespace(htid) + "."
8780
end
8881
end
8982
end

spec/ht/pairtree_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ def make_paths(idmap)
4848
describe "#create" do
4949
let(:pt) { HathiTrust::Pairtree.new(root: @tmpdir + "sdr1/obj") }
5050

51-
it "can create new object directories" do
51+
it "can create new object directories in the expected location" do
5252
expect { pt.create("test.something", new_namespace_allowed: false) }.to raise_error(HathiTrust::Pairtree::NamespaceDoesNotExist)
53-
expect(pt.create("test.something", new_namespace_allowed: true).exist?(".")).to be true
53+
54+
pt_obj = pt.create("test.something", new_namespace_allowed: true)
55+
expect(pt_obj.exist?(".")).to be true
56+
expect(pt_obj.to_path).to eq((@tmpdir + "sdr1/obj/test/pairtree_root/so/me/th/in/g/something").to_s)
5457
end
5558

5659
it "create is idempotent" do

0 commit comments

Comments
 (0)