Class: Pdfrb::Compare::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/compare/report.rb

Overview

Result of comparing two PDFs. Immutable value object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_count_delta:, per_page_text_diffs:, font_diff:, image_count_delta:, outline_diff:, metadata_diff:, structural_diffs:) ⇒ Report

Returns a new instance of Report.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pdfrb/compare/report.rb', line 11

def initialize(page_count_delta:, per_page_text_diffs:, font_diff:,
               image_count_delta:, outline_diff:, metadata_diff:,
               structural_diffs:)
  @page_count_delta = page_count_delta
  @per_page_text_diffs = per_page_text_diffs.freeze
  @font_diff = font_diff.freeze
  @image_count_delta = image_count_delta
  @outline_diff = outline_diff.freeze
  @metadata_diff = .freeze
  @structural_diffs = structural_diffs.freeze
  freeze
end

Instance Attribute Details

#font_diffObject (readonly)

Returns the value of attribute font_diff.



7
8
9
# File 'lib/pdfrb/compare/report.rb', line 7

def font_diff
  @font_diff
end

#image_count_deltaObject (readonly)

Returns the value of attribute image_count_delta.



7
8
9
# File 'lib/pdfrb/compare/report.rb', line 7

def image_count_delta
  @image_count_delta
end

#metadata_diffObject (readonly)

Returns the value of attribute metadata_diff.



7
8
9
# File 'lib/pdfrb/compare/report.rb', line 7

def 
  @metadata_diff
end

#outline_diffObject (readonly)

Returns the value of attribute outline_diff.



7
8
9
# File 'lib/pdfrb/compare/report.rb', line 7

def outline_diff
  @outline_diff
end

#page_count_deltaObject (readonly)

Returns the value of attribute page_count_delta.



7
8
9
# File 'lib/pdfrb/compare/report.rb', line 7

def page_count_delta
  @page_count_delta
end

#per_page_text_diffsObject (readonly)

Returns the value of attribute per_page_text_diffs.



7
8
9
# File 'lib/pdfrb/compare/report.rb', line 7

def per_page_text_diffs
  @per_page_text_diffs
end

#structural_diffsObject (readonly)

Returns the value of attribute structural_diffs.



7
8
9
# File 'lib/pdfrb/compare/report.rb', line 7

def structural_diffs
  @structural_diffs
end

Instance Method Details

#equivalent?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
# File 'lib/pdfrb/compare/report.rb', line 24

def equivalent?
  return false unless @page_count_delta.zero?
  return false unless @per_page_text_diffs.empty?
  return false unless @font_diff[:added].empty? && @font_diff[:removed].empty?
  return false unless @image_count_delta.zero?
  return false unless @structural_diffs.empty?

  true
end

#similarityObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pdfrb/compare/report.rb', line 34

def similarity
  total_checks = 0
  passing = 0

  total_checks += 1
  passing += 1 if @page_count_delta.zero?

  total_checks += @per_page_text_diffs.length + 1
  passing += @per_page_text_diffs.count { |d| d[:similarity] > 0.95 } + 1

  total_checks += 1
  passing += 1 if @font_diff[:added].empty? && @font_diff[:removed].empty?

  total_checks += 1
  passing += 1 if @image_count_delta.zero?

  total_checks + 1
  passing + (@structural_diffs.empty? ? 1 : 0)
  (passing.to_f / (total_checks + 1)).round(4)
end

#summaryObject



69
70
71
72
73
74
75
# File 'lib/pdfrb/compare/report.rb', line 69

def summary
  status = equivalent? ? "EQUIVALENT" : "DIFFERENT"
  sim = (similarity * 100).round(1)
  "#{status} (#{sim}% similarity, page_delta=#{@page_count_delta}, " \
    "text_diffs=#{@per_page_text_diffs.length}, " \
    "font_changes=#{@font_diff[:added].length + @font_diff[:removed].length})"
end

#to_hObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pdfrb/compare/report.rb', line 55

def to_h
  {
    equivalent: equivalent?,
    similarity: similarity,
    page_count_delta: @page_count_delta,
    per_page_text_diffs: @per_page_text_diffs,
    font_diff: @font_diff,
    image_count_delta: @image_count_delta,
    outline_diff: @outline_diff,
    metadata_diff: @metadata_diff,
    structural_diffs: @structural_diffs,
  }
end