Class: Pdfh::PdfFileHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfh/utils/pdf_file_handler.rb

Overview

Handles the PDF file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, type) ⇒ self

Parameters:



11
12
13
14
# File 'lib/pdfh/utils/pdf_file_handler.rb', line 11

def initialize(file, type)
  @file = file
  @type = type
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



6
7
8
# File 'lib/pdfh/utils/pdf_file_handler.rb', line 6

def document
  @document
end

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/pdfh/utils/pdf_file_handler.rb', line 6

def file
  @file
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/pdfh/utils/pdf_file_handler.rb', line 6

def type
  @type
end

Instance Method Details

#base_nameString

Returns:



62
63
64
# File 'lib/pdfh/utils/pdf_file_handler.rb', line 62

def base_name
  File.basename(file)
end

#copy_companion_files(destination, document) ⇒ void

This method returns an undefined value.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pdfh/utils/pdf_file_handler.rb', line 48

def copy_companion_files(destination, document)
  Pdfh.debug "~~~~~~~~~~~~~~~~~~ Writing Companion files"
  document.companion_files.each do |file|
    Pdfh.debug "  Working on #{file.inspect}..."
    src_name = File.join(document.home_dir, file)
    src_ext = File.extname(file)
    dest_name = File.basename(document.new_name, ".pdf")
    dest_full = File.join(destination, "#{dest_name}#{src_ext}")
    Pdfh.debug "    cp #{src_name} --> #{dest_full}"
    FileUtils.cp(src_name, dest_full) unless Pdfh.dry?
  end
end

#make_document_backup(document) ⇒ void

This method returns an undefined value.

Create a backup of original document



38
39
40
41
42
43
44
45
# File 'lib/pdfh/utils/pdf_file_handler.rb', line 38

def make_document_backup(document)
  Pdfh.debug "~~~~~~~~~~~~~~~~~~ Creating PDF backup"
  Dir.chdir(document.home_dir) do
    Pdfh.debug "  Working on: #{document.home_dir.inspect} directory"
    Pdfh.debug "    mv #{document.file_name.inspect} -> #{document.backup_name.inspect}"
    File.rename(document.file_name, document.backup_name) unless Pdfh.dry?
  end
end

#process_document(base_path) ⇒ void

This method returns an undefined value.

Generate document, and process actions



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pdfh/utils/pdf_file_handler.rb', line 23

def process_document(base_path)
  Pdfh.info "Working on #{base_name.colorize(:light_green)}"
  raise IOError, "File #{file} not found" unless File.exist?(file)

  @document = Document.new(file, type, extract_text)
  document.print_info
  write_pdf(base_path)

  nil
rescue StandardError => e
  Pdfh.ident_print "Doc Error", e.message, color: :red, width: 12
end

#type?boolean

Returns:

  • (boolean)


17
18
19
# File 'lib/pdfh/utils/pdf_file_handler.rb', line 17

def type?
  !!type
end