Class: ROF::Utility
- Inherits:
-
Object
- Object
- ROF::Utility
- Defined in:
- lib/rof/utility.rb
Overview
A few common utility methods
Constant Summary collapse
- WORK_TYPE_WITH_PREFIX_PATTERN =
/^[Ww]ork(-(.+))?/- WORK_TYPES =
Strictly speaking, a Collection is not a Work- it’s included here to cull out and pass down the batch processing pipeline
{ # csv name => af-model 'article' => 'Article', 'dataset' => 'Dataset', 'document' => 'Document', 'collection' => 'Collection', 'etd' => 'Etd', 'image' => 'Image', 'gtar' => 'Gtar', 'osfarchive' => 'OsfArchive' }.freeze
Instance Attribute Summary collapse
-
#workdir ⇒ Object
readonly
give base directory of given file for workdir.
Class Method Summary collapse
-
.file_from_targz(targzfile, file_name) ⇒ Object
read file from gzipped tar archive.
-
.prop_ds(owner, representative = nil) ⇒ Object
set ‘properties’.
Instance Method Summary collapse
-
#decode_work_type(obj) ⇒ Object
Given an object’s type, detrmine and return its af-model.
-
#initialize ⇒ Utility
constructor
A new instance of Utility.
-
#next_label ⇒ Object
Issue pid label.
-
#set_workdir(filename) ⇒ Object
use base directory of given file for workdir.
Constructor Details
#initialize ⇒ Utility
Returns a new instance of Utility.
8 9 10 11 |
# File 'lib/rof/utility.rb', line 8 def initialize @seq = 0 @workdir = '.' end |
Instance Attribute Details
#workdir ⇒ Object (readonly)
give base directory of given file for workdir
37 38 39 |
# File 'lib/rof/utility.rb', line 37 def workdir @workdir end |
Class Method Details
.file_from_targz(targzfile, file_name) ⇒ Object
read file from gzipped tar archive
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rof/utility.rb', line 67 def self.file_from_targz(targzfile, file_name) File.open(targzfile, 'rb') do |file| Zlib::GzipReader.wrap(file) do |gz| Gem::Package::TarReader.new(gz) do |tar| tar.seek(file_name) do |file_entry| file_dest_dir = File.join(File.dirname(targzfile), File.dirname(file_entry.full_name)) FileUtils.mkdir_p(file_dest_dir) File.open(File.join(file_dest_dir, File.basename(file_name)), 'wb') do |file_handle| file_handle.write(file_entry.read) end end tar.close end end end end |
.prop_ds(owner, representative = nil) ⇒ Object
set ‘properties’
57 58 59 60 61 62 63 64 |
# File 'lib/rof/utility.rb', line 57 def self.prop_ds(owner, representative = nil) s = "<fields><depositor>batch_ingest</depositor>\n<owner>#{owner}</owner>\n" if representative s += "<representative>#{representative}</representative>\n" end s += "</fields>\n" s end |
Instance Method Details
#decode_work_type(obj) ⇒ Object
Given an object’s type, detrmine and return its af-model
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rof/utility.rb', line 40 def decode_work_type(obj) if obj['type'] =~ WORK_TYPE_WITH_PREFIX_PATTERN return 'GenericWork' if Regexp.last_match(2).nil? Regexp.last_match(2) else # this will return nil if key t does not exist work_type = obj['type'].downcase WORK_TYPES[work_type] end end |
#next_label ⇒ Object
Issue pid label
52 53 54 |
# File 'lib/rof/utility.rb', line 52 def next_label "$(pid--#{@seq})".tap { |_| @seq += 1 } end |
#set_workdir(filename) ⇒ Object
use base directory of given file for workdir
32 33 34 |
# File 'lib/rof/utility.rb', line 32 def set_workdir(filename) @workdir = File.dirname(filename) end |