class PdfMatcher::Matcher

Attributes

diff_pdf_opts[R]
output_diff_path[R]
pdf1[R]
pdf2[R]

Public Class Methods

new(pdf1, pdf2, output_diff: nil, diff_pdf_opts: nil) click to toggle source
# File lib/pdf_matcher/matcher.rb, line 9
def initialize(pdf1, pdf2, output_diff: nil, diff_pdf_opts: nil)
  @pdf1 = PdfFile.init(pdf1)
  @pdf2 = PdfFile.init(pdf2)
  @output_diff_path = output_diff ? Pathname(output_diff) : nil
  @diff_pdf_opts = diff_pdf_opts || PdfMatcher.config.diff_pdf_opts
end

Public Instance Methods

match() click to toggle source
# File lib/pdf_matcher/matcher.rb, line 16
def match
  @result ||= begin
    matched = match_pdfs

    if matched
      # [NOTE] The diff-pdf command will generate a diff PDF file
      # regardless of the result if the `--output_diff` option is given.
      output_diff_path.unlink if output_diff_path&.exist?
      MatchResult.new(matched, pdf1, pdf2, nil)
    else
      MatchResult.new(matched, pdf1, pdf2, output_diff_path)
    end
  end
end

Private Instance Methods

match_pdfs() click to toggle source
# File lib/pdf_matcher/matcher.rb, line 39
def match_pdfs
  open_pdf_files do |pdf1_path, pdf2_path|
    DiffPdf.exec(
      pdf1_path, pdf2_path,
      output_diff: output_diff_path,
      options: diff_pdf_opts
    )
  end
end
open_pdf_files() { |path, path| ... } click to toggle source
# File lib/pdf_matcher/matcher.rb, line 49
def open_pdf_files
  pdfs.each(&:open)
  yield(pdf1.path, pdf2.path)
ensure
  pdfs.each(&:close)
end
pdfs() click to toggle source
# File lib/pdf_matcher/matcher.rb, line 35
def pdfs
  @pdfs ||= [pdf1, pdf2]
end