class Pdfh::Document

Handles the PDF detected by the rules

Attributes

extra[R]
file[R]
pdf_doc[R]
text[R]
type[R]

Public Class Methods

new(file, type) click to toggle source

@param file [String] @param type [DocumentType] @return [self]

# File lib/pdfh/document.rb, line 11
def initialize(file, type)
  raise IOError, "File #{file} not found" unless File.exist?(file)

  @file = file
  @type = type
  Pdfh.verbose_print "=== Type: #{type.name} =============================="
  @pdf_doc = PdfHandler.new(file, type.pwd)
  @text = @pdf_doc.extract_text
  Pdfh.verbose_print "~~~~~~~~~~~~~~~~~~ Finding a subtype"
  @sub_type = type.sub_type(@text)
  Pdfh.verbose_print "  SubType: #{@sub_type}"
  @companion = search_companion_files

  month, year, @extra = match_data
  @period = DocumentPeriod.new(day: extra, month: month, month_offset: @sub_type&.month_offset, year: year)
  Pdfh.verbose_print "  Period: #{@period.inspect}"
end

Public Instance Methods

backup_name() click to toggle source

@return [String]

# File lib/pdfh/document.rb, line 40
def backup_name
  "#{file_name}.bkp"
end
companion_files(join: false) click to toggle source
# File lib/pdfh/document.rb, line 84
def companion_files(join: false)
  return @companion unless join

  @companion.empty? ? "N/A" : @companion.join(", ")
end
file_name() click to toggle source

@return [String]

# File lib/pdfh/document.rb, line 35
def file_name
  File.basename(@file)
end
file_name_only() click to toggle source

@return [String]

# File lib/pdfh/document.rb, line 30
def file_name_only
  File.basename(@file, File.extname(@file))
end
home_dir() click to toggle source

@return [String]

# File lib/pdfh/document.rb, line 91
def home_dir
  File.dirname(@file)
end
new_name() click to toggle source

@return [String]

# File lib/pdfh/document.rb, line 60
def new_name
  template = @type.name_template
  new_name = template
             .sub("{original}", file_name_only)
             .sub("{period}", period)
             .sub("{type}", type_name)
             .sub("{subtype}", sub_type)
             .sub("{extra}", extra || "")
  "#{new_name}.pdf"
end
period() click to toggle source

@return [String]

# File lib/pdfh/document.rb, line 55
def period
  @period.to_s
end
print_cmd() click to toggle source

@return [String]

store_path() click to toggle source

@return [String]

# File lib/pdfh/document.rb, line 72
def store_path
  @type.store_path.gsub("{YEAR}", @period.year.to_s)
end
sub_type() click to toggle source

@return [String]

# File lib/pdfh/document.rb, line 50
def sub_type
  @sub_type&.name&.titleize || "N/A"
end
to_s() click to toggle source

@return [String]

# File lib/pdfh/document.rb, line 96
def to_s
  @file
end
type_name() click to toggle source

@return [String]

# File lib/pdfh/document.rb, line 45
def type_name
  @type&.name&.titleize || "N/A"
end

Private Instance Methods

match_data() click to toggle source

named matches can appear in any order with names 'd', 'm' and 'y' unnamed matches needs to be in order month, year @return [Array] - format [month, year, day]

# File lib/pdfh/document.rb, line 105
def match_data
  Pdfh.verbose_print "~~~~~~~~~~~~~~~~~~ RegEx"
  Pdfh.verbose_print "  Using regex: #{@type.re_date}"
  Pdfh.verbose_print "        named:   #{@type.re_date.named_captures}"
  matched = @type.re_date.match(@text)
  raise ReDateError unless matched

  Pdfh.verbose_print "     captured: #{matched.captures}"

  return matched.captures.map(&:downcase) if @type.re_date.named_captures.empty?

  extra = matched.captures.size > 2 ? matched[:d] : nil
  [matched[:m].downcase, matched[:y], extra]
end
search_companion_files() click to toggle source

@return [Array]

# File lib/pdfh/document.rb, line 121
def search_companion_files
  Pdfh.verbose_print "~~~~~~~~~~~~~~~~~~ Searching Companion files"
  Pdfh.verbose_print "  Searching on: #{home_dir.inspect}"
  Dir.chdir(home_dir) do
    files_matching = Dir["#{file_name_only}.*"]
    companion = files_matching.reject { |file| file.include? ".pdf" }
    Pdfh.verbose_print "    Found: #{companion.inspect}"

    companion
  end
end