Skip to content

Commit 99696c5

Browse files
committed
Deal with new namespaces on creation
1 parent 5e653c8 commit 99696c5

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/ht/pairtree.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# coding: utf-8
12
require "ht/pairtree/version"
23

34
# Pairtree is ancient and throws warnings so we suppress them on require
@@ -16,6 +17,9 @@ class Pairtree
1617
class PairtreeError < StandardError;
1718
end
1819

20+
class NamespaceDoesNotExist < PairtreeError
21+
end
22+
1923
SDR_DATA_ROOT_DEFAULT = (ENV["SDRDATAROOT"] || '/sdr1') + '/obj'
2024

2125
# Create a new pairtree object rooted at the given directory
@@ -46,13 +50,25 @@ def pairtree_for(htid)
4650

4751
# Create a pairtree for the given htid. Allow namespace creation
4852
# only if told to.
49-
# @param [String] htid The HTID
53+
# @param htid [String] The HTID
54+
# @param new_namespace_allowed [Boolean] Whether or not to error if the namespace DNE
55+
# @raise [NamespaceDoesNotExist] if namespace DNE and new namespace not allowed
5056
# @return [Pairtree::Obj] the underlying pairtree object
51-
def create(htid, create_new_namespace: false)
52-
create_namespace_dir(htid) if create_new_namespace
57+
def create(htid, new_namespace_allowed: false)
58+
if !namespace_exists?(htid)
59+
if new_namespace_allowed
60+
create_namespace_dir(htid)
61+
else
62+
raise NamespaceDoesNotExist.new("Namespace #{namespace(htid)} does not exist")
63+
end
64+
end
5365
pairtree_for(htid).mk(htid)
5466
end
5567

68+
def namespace_exists?(htid)
69+
namespace_dir(htid).exists?
70+
end
71+
5672

5773
def create_namespace_dir(htid)
5874
ndir = namespace_dir(htid)

0 commit comments

Comments
 (0)