Class: Pdfmdsort
Overview
Class: pdfmdsort
TODO: Author values with a slave One/two should be sorted into one/two/yyyymmdd-one_to-xxx.pdf
Instance Attribute Summary collapse
-
#copy ⇒ Object
Returns the value of attribute copy.
-
#destination ⇒ Object
Returns the value of attribute destination.
-
#dryrun ⇒ Object
Returns the value of attribute dryrun.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#interactive ⇒ Object
Returns the value of attribute interactive.
-
#overwrite ⇒ Object
Returns the value of attribute overwrite.
Attributes inherited from Pdfmd
Instance Method Summary collapse
-
#checkDestination ⇒ Object
Check if the destination is valid.
-
#get_author ⇒ Object
Get the author Return ‘false’ if no author is being found.
-
#initialize(input) ⇒ Pdfmdsort
constructor
Initialize.
-
#sort ⇒ Object
Sort the file away.
Methods inherited from Pdfmd
#check_metatags, #metadata, #readUserInput, #read_metatags
Methods included from Pdfmdmethods
#determineValidSetting, #log, #queryHiera
Constructor Details
#initialize(input) ⇒ Pdfmdsort
Initialize
9 10 11 12 13 14 15 16 |
# File 'lib/pdfmd/pdfmdsort.rb', line 9 def initialize(input) super input @destination = '.' @interactive = false @copy = false @dryrun = false @overwrite = false end |
Instance Attribute Details
#copy ⇒ Object
Returns the value of attribute copy.
6 7 8 |
# File 'lib/pdfmd/pdfmdsort.rb', line 6 def copy @copy end |
#destination ⇒ Object
Returns the value of attribute destination.
6 7 8 |
# File 'lib/pdfmd/pdfmdsort.rb', line 6 def destination @destination end |
#dryrun ⇒ Object
Returns the value of attribute dryrun.
6 7 8 |
# File 'lib/pdfmd/pdfmdsort.rb', line 6 def dryrun @dryrun end |
#filename ⇒ Object
Returns the value of attribute filename.
6 7 8 |
# File 'lib/pdfmd/pdfmdsort.rb', line 6 def filename @filename end |
#interactive ⇒ Object
Returns the value of attribute interactive.
6 7 8 |
# File 'lib/pdfmd/pdfmdsort.rb', line 6 def interactive @interactive end |
#overwrite ⇒ Object
Returns the value of attribute overwrite.
6 7 8 |
# File 'lib/pdfmd/pdfmdsort.rb', line 6 def overwrite @overwrite end |
Instance Method Details
#checkDestination ⇒ Object
Check if the destination is valid
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/pdfmd/pdfmdsort.rb', line 21 def checkDestination log('debug', "Checking destination parameter '#{@destination}'.") if File.file?(@destination) log('error', "Destination '#{@destination}' is a file.") false else log('debug', "Destination '#{@destination}' as directory confirmed.") true end end |
#get_author ⇒ Object
Get the author Return ‘false’ if no author is being found.
38 39 40 41 42 43 44 45 |
# File 'lib/pdfmd/pdfmdsort.rb', line 38 def () if not self.('author') return false end = ['author'].gsub(/\./,'_').gsub(/\&/,'').gsub(/\-/,'_').gsub(/\s/,'_').gsub(/\,/,'_').gsub(/\_\_/,'_') I18n.enforce_available_locales = false I18n.transliterate().downcase # Normalising end |
#sort ⇒ Object
Sort the file away
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/pdfmd/pdfmdsort.rb', line 50 def sort if self.checkDestination if @interactive answer = readUserInput("Process '#{@filename}' ([y]/n): ") answer = answer.empty? ? 'y' : answer self.log('info', "User Answer for file '#{@filename}': #{answer}") if !answer.match(/y/) self.log('info',"Skipping file '#{@filename}' due to user answer: '#{answer}'.") return else self.log('info',"Processing file '#{@filename}' due to user answer: '#{answer}'.") end end if not = () or .empty? self.log('error', "File '#{@filename}' has not value for author set. Cannot sort file. Abort.") exit 1 end targetdir = @destination.chomp + '/' + targetfile = targetdir + '/' + Pathname.new(@filename).basename.to_s # Create the target dir if not existing. if !File.exists? targetdir if @dryrun self.log('info', "Dryrun: Created Directory '#{targetdir}'.") else self.log('info', "Created directory '#{targetdir}'.") puts targetdir FileUtils.mkdir_p(targetdir) end end # Check if the file already exists # This does nothing so far if File.exists?(targetfile) and @overwrite self.log('info', "File '#{@filename}' already exists. Overwrite active: replacing file.") elsif File.exists?(targetfile) and !@overwrite self.log('info', "File '#{@filename}' already exists, overwrite disabled: not replacing file.") return true end if @copy if @dryrun self.log('info', "Dryrun: Copy file '#{@filename}' to '#{targetdir}'.") else self.log('info', "Copy file '#{@filename}' to '#{targetdir}'.") FileUtils.cp(@filename, targetdir) end else if @dryrun self.log('info', "Dryrun: Move file '#{@filename}' to '#{targetdir}'.") else self.log('info', "Move file '#{@filename}' to '#{targetdir}'.") FileUtils.mv(@filename, targetdir) end end end end |