Class: SnapDiff::ComparisonResult

Inherits:
Struct
  • Object
show all
Defined in:
lib/snap_diff/comparison_result.rb

Overview

Represents the result of comparing two images

This value object encapsulates the result of an image comparison operation. It follows the Single Responsibility Principle by focusing solely on representing the difference state, including:

  • Whether images are different or equal
  • Why they differ (dimensions, pixels, etc.)
  • The specific region of difference
  • Whether differences are tolerable based on configured thresholds

As part of the layered comparison architecture, this class represents the final output of the comparison process, containing all data needed for reporting.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_image_pathObject

Returns the value of attribute base_image_path

Returns:

  • (Object)

    the current value of base_image_path



18
19
20
# File 'lib/snap_diff/comparison_result.rb', line 18

def base_image_path
  @base_image_path
end

#comparisonObject

Returns the value of attribute comparison

Returns:

  • (Object)

    the current value of comparison



18
19
20
# File 'lib/snap_diff/comparison_result.rb', line 18

def comparison
  @comparison
end

#failed_byObject

Returns the value of attribute failed_by

Returns:

  • (Object)

    the current value of failed_by



18
19
20
# File 'lib/snap_diff/comparison_result.rb', line 18

def failed_by
  @failed_by
end

#image_pathObject

Returns the value of attribute image_path

Returns:

  • (Object)

    the current value of image_path



18
19
20
# File 'lib/snap_diff/comparison_result.rb', line 18

def image_path
  @image_path
end

#metaObject

Returns the value of attribute meta

Returns:

  • (Object)

    the current value of meta



18
19
20
# File 'lib/snap_diff/comparison_result.rb', line 18

def meta
  @meta
end

#regionObject

Returns the value of attribute region

Returns:

  • (Object)

    the current value of region



18
19
20
# File 'lib/snap_diff/comparison_result.rb', line 18

def region
  @region
end

Class Method Details

.build_null(comparison, base_image_path, new_image_path, failed_by = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/snap_diff/comparison_result.rb', line 19

def self.build_null(comparison, base_image_path, new_image_path, failed_by = nil)
  ComparisonResult.new(
    nil,
    {difference_level: nil, max_color_distance: 0},
    comparison,
    failed_by,
    base_image_path,
    new_image_path
  ).freeze
end

Instance Method Details

#area_size_limitObject



54
55
56
# File 'lib/snap_diff/comparison_result.rb', line 54

def area_size_limit
  options[:area_size_limit]
end

#blank?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/snap_diff/comparison_result.rb', line 58

def blank?
  region.nil? || region_area_size.zero?
end

#coordinatesObject



76
77
78
# File 'lib/snap_diff/comparison_result.rb', line 76

def coordinates
  region&.to_edge_coordinates
end

#diff_maskObject



102
103
104
# File 'lib/snap_diff/comparison_result.rb', line 102

def diff_mask
  meta[:diff_mask]
end

#different?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/snap_diff/comparison_result.rb', line 30

def different?
  failed? || !(blank? || tolerable?)
end

#equal?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/snap_diff/comparison_result.rb', line 34

def equal?
  !different?
end

#failed?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/snap_diff/comparison_result.rb', line 38

def failed?
  !!failed_by
end

#inspectObject

One-line debugging summary with the difference metrics. (Error messages use #to_h — see Reporters::Default#build_error_message.)



82
83
84
85
86
87
# File 'lib/snap_diff/comparison_result.rb', line 82

def inspect
  "#<#{self.class.name} different=#{different?} failed_by=#{failed_by.inspect} " \
    "area_size=#{region_area_size} region=#{coordinates.inspect} " \
    "difference_level=#{ratio.inspect} " \
    "base=#{original_image_path} new=#{new_image_path}>"
end

#new_image_pathObject

Path accessors for backward compatibility



94
95
96
# File 'lib/snap_diff/comparison_result.rb', line 94

def new_image_path
  image_path || comparison&.new_image_path
end

#optionsObject



42
43
44
# File 'lib/snap_diff/comparison_result.rb', line 42

def options
  comparison.options
end

#original_image_pathObject



98
99
100
# File 'lib/snap_diff/comparison_result.rb', line 98

def original_image_path
  base_image_path || comparison&.base_image_path
end

#ratioObject



66
67
68
# File 'lib/snap_diff/comparison_result.rb', line 66

def ratio
  meta[:difference_level]
end

#region_area_sizeObject



62
63
64
# File 'lib/snap_diff/comparison_result.rb', line 62

def region_area_size
  @region_area_size ||= region&.size || 0
end

#skip_areaObject



50
51
52
# File 'lib/snap_diff/comparison_result.rb', line 50

def skip_area
  comparison.skip_area
end

#to_hObject

Serializable difference metrics. The raw diff mask image is excluded — it is an image object, not a metric, and is reachable via #diff_mask.



72
73
74
# File 'lib/snap_diff/comparison_result.rb', line 72

def to_h
  {area_size: region_area_size, region: coordinates}.merge!(meta.except(:diff_mask))
end

#tolerable?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/snap_diff/comparison_result.rb', line 89

def tolerable?
  !!((area_size_limit && area_size_limit >= region_area_size) || (tolerance && tolerance >= ratio))
end

#toleranceObject



46
47
48
# File 'lib/snap_diff/comparison_result.rb', line 46

def tolerance
  options[:tolerance]
end