Class: DruidTools::Druid
- Inherits:
-
Object
- Object
- DruidTools::Druid
- Defined in:
- lib/druid_tools/druid.rb
Direct Known Subclasses
Constant Summary collapse
- STRICT_LET =
See consul.stanford.edu/pages/viewpage.action?title=SURI+2.0+Specification&spaceKey=chimera character class matching allowed letters in a druid suitable for use in regex (no aeioul)
'[b-df-hjkmnp-tv-z]'
Class Attribute Summary collapse
-
.prefix ⇒ Object
Returns the value of attribute prefix.
Instance Attribute Summary collapse
-
#base ⇒ Object
Returns the value of attribute base.
-
#druid ⇒ Object
Returns the value of attribute druid.
Class Method Summary collapse
-
.glob ⇒ String
Suitable for use in [Dir#glob].
-
.pattern(strict = false) ⇒ Regexp
Matches druid:aa111aa1111 or aa111aa1111.
-
.strict_glob ⇒ String
Suitable for use in [Dir#glob].
-
.valid?(druid, strict = false) ⇒ Boolean
True if druid matches pattern; otherwise false.
Instance Method Summary collapse
- #base_pathname ⇒ Object
- #content_dir(create = true) ⇒ Object
- #find(type, path) ⇒ Object
- #find_content(path) ⇒ Object
-
#find_filelist_parent(type, filelist) ⇒ Pathname
Search for and return the pathname of the directory that contains the list of files.
- #find_metadata(path) ⇒ Object
- #find_temp(path) ⇒ Object
- #id ⇒ Object
-
#initialize(druid, base = '.', strict = false) ⇒ Druid
constructor
A new instance of Druid.
- #metadata_dir(create = true) ⇒ Object
- #mkdir(extra = nil) ⇒ Object
- #path(extra = nil, create = false) ⇒ Object
- #pathname ⇒ Object
- #pruning_base ⇒ Object
- #temp_dir(create = true) ⇒ Object
- #tree ⇒ Object
Constructor Details
#initialize(druid, base = '.', strict = false) ⇒ Druid
Returns a new instance of Druid.
71 72 73 74 75 76 77 78 |
# File 'lib/druid_tools/druid.rb', line 71 def initialize(druid, base = '.', strict = false) druid = druid.to_s unless druid.is_a? String raise ArgumentError, "Invalid DRUID: '#{druid}'" unless self.class.valid?(druid, strict) druid = [self.class.prefix, druid].join(':') unless druid =~ /^#{self.class.prefix}:/ @base = base @druid = druid end |
Class Attribute Details
.prefix ⇒ Object
Returns the value of attribute prefix.
15 16 17 |
# File 'lib/druid_tools/druid.rb', line 15 def prefix @prefix end |
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
8 9 10 |
# File 'lib/druid_tools/druid.rb', line 8 def base @base end |
#druid ⇒ Object
Returns the value of attribute druid.
8 9 10 |
# File 'lib/druid_tools/druid.rb', line 8 def druid @druid end |
Class Method Details
.glob ⇒ String
Returns suitable for use in [Dir#glob].
26 27 28 |
# File 'lib/druid_tools/druid.rb', line 26 def glob "{#{prefix}:,}[a-z][a-z][0-9][0-9][0-9][a-z][a-z][0-9][0-9][0-9][0-9]" end |
.pattern(strict = false) ⇒ Regexp
Returns matches druid:aa111aa1111 or aa111aa1111.
19 20 21 22 23 |
# File 'lib/druid_tools/druid.rb', line 19 def pattern(strict = false) return /\A(?:#{prefix}:)?(#{STRICT_LET}{2})(\d{3})(#{STRICT_LET}{2})(\d{4})\z/ if strict /\A(?:#{prefix}:)?([a-z]{2})(\d{3})([a-z]{2})(\d{4})\z/ end |
.strict_glob ⇒ String
Returns suitable for use in [Dir#glob].
31 32 33 |
# File 'lib/druid_tools/druid.rb', line 31 def strict_glob "{#{prefix}:,}#{STRICT_LET}#{STRICT_LET}[0-9][0-9][0-9]#{STRICT_LET}#{STRICT_LET}[0-9][0-9][0-9][0-9]" end |
.valid?(druid, strict = false) ⇒ Boolean
Returns true if druid matches pattern; otherwise false.
38 39 40 |
# File 'lib/druid_tools/druid.rb', line 38 def valid?(druid, strict = false) druid =~ pattern(strict) ? true : false end |
Instance Method Details
#base_pathname ⇒ Object
131 132 133 |
# File 'lib/druid_tools/druid.rb', line 131 def base_pathname Pathname base end |
#content_dir(create = true) ⇒ Object
44 45 46 |
# File 'lib/druid_tools/druid.rb', line 44 def content_dir(create = true) path('content', create) end |
#find(type, path) ⇒ Object
102 103 104 105 106 |
# File 'lib/druid_tools/druid.rb', line 102 def find(type, path) possibles = [self.path(type.to_s), self.path, File.('..', self.path)] loc = possibles.find { |p| File.exist?(File.join(p, path)) } loc.nil? ? nil : File.join(loc, path) end |
#find_content(path) ⇒ Object
56 57 58 |
# File 'lib/druid_tools/druid.rb', line 56 def find_content(path) find(:content, path) end |
#find_filelist_parent(type, filelist) ⇒ Pathname
Returns Search for and return the pathname of the directory that contains the list of files. Raises an exception unless a directory is found that contains all the files in the list.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/druid_tools/druid.rb', line 112 def find_filelist_parent(type, filelist) raise 'File list not specified' if filelist.nil? || filelist.empty? filelist = [filelist] unless filelist.is_a?(Array) search_dir = Pathname(path(type)) directories = [search_dir, search_dir.parent, search_dir.parent.parent] found_dir = directories.find { |pathname| pathname.join(filelist[0]).exist? } raise "#{type} dir not found for '#{filelist[0]}' when searching '#{search_dir}'" if found_dir.nil? filelist.each do |filename| raise "File '#{filename}' not found in #{type} dir '#{found_dir}'" unless found_dir.join(filename).exist? end found_dir end |
#find_metadata(path) ⇒ Object
60 61 62 |
# File 'lib/druid_tools/druid.rb', line 60 def (path) find(:metadata, path) end |
#find_temp(path) ⇒ Object
64 65 66 |
# File 'lib/druid_tools/druid.rb', line 64 def find_temp(path) find(:temp, path) end |
#id ⇒ Object
80 81 82 |
# File 'lib/druid_tools/druid.rb', line 80 def id @druid.scan(self.class.pattern).flatten.join end |
#metadata_dir(create = true) ⇒ Object
48 49 50 |
# File 'lib/druid_tools/druid.rb', line 48 def (create = true) path('metadata', create) end |
#mkdir(extra = nil) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/druid_tools/druid.rb', line 94 def mkdir(extra = nil) new_path = path(extra) raise DruidTools::DifferentContentExistsError, "Unable to create directory, link already exists: #{new_path}" if File.symlink? new_path raise DruidTools::SameContentExistsError, "The directory already exists: #{new_path}" if File.directory? new_path FileUtils.mkdir_p(new_path) end |
#path(extra = nil, create = false) ⇒ Object
88 89 90 91 92 |
# File 'lib/druid_tools/druid.rb', line 88 def path(extra = nil, create = false) result = File.join(*[base, tree, extra].compact) mkdir(extra) if create && !File.exist?(result) result end |
#pathname ⇒ Object
127 128 129 |
# File 'lib/druid_tools/druid.rb', line 127 def pathname Pathname path end |
#pruning_base ⇒ Object
135 136 137 |
# File 'lib/druid_tools/druid.rb', line 135 def pruning_base pathname.parent end |
#temp_dir(create = true) ⇒ Object
52 53 54 |
# File 'lib/druid_tools/druid.rb', line 52 def temp_dir(create = true) path('temp', create) end |
#tree ⇒ Object
84 85 86 |
# File 'lib/druid_tools/druid.rb', line 84 def tree @druid.scan(self.class.pattern).flatten + [id] end |