@@ -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
8982end
0 commit comments