Class: Canon::Comparison::ComparisonResult

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/comparison/comparison_result.rb

Overview

Encapsulates the result of a comparison operation Provides methods to query equivalence based on normative diffs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(differences:, preprocessed_strings:, format:, html_version: nil, match_options: nil, algorithm: :dom, original_strings: nil) ⇒ ComparisonResult

Returns a new instance of ComparisonResult.

Parameters:

  • differences (Array<DiffNode>)

    Array of difference nodes

  • preprocessed_strings (Array<String, String>)

    Pre-processed content for display

  • format (Symbol)

    Format type (:xml, :html, :json, :yaml)

  • html_version (Symbol, nil) (defaults to: nil)

    HTML version (:html4 or :html5) for HTML format only

  • match_options (Hash, nil) (defaults to: nil)

    Resolved match options used for comparison

  • algorithm (Symbol) (defaults to: :dom)

    Diff algorithm used (:dom or :semantic)

  • original_strings (Array<String, String>, nil) (defaults to: nil)

    Original unprocessed content for line diff



18
19
20
21
22
23
24
25
26
27
# File 'lib/canon/comparison/comparison_result.rb', line 18

def initialize(differences:, preprocessed_strings:, format:,
html_version: nil, match_options: nil, algorithm: :dom, original_strings: nil)
  @differences = differences
  @preprocessed_strings = preprocessed_strings
  @original_strings = original_strings || preprocessed_strings
  @format = format
  @html_version = html_version
  @match_options = match_options
  @algorithm = algorithm
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



8
9
10
# File 'lib/canon/comparison/comparison_result.rb', line 8

def algorithm
  @algorithm
end

#differencesObject (readonly)

Returns the value of attribute differences.



8
9
10
# File 'lib/canon/comparison/comparison_result.rb', line 8

def differences
  @differences
end

#formatObject (readonly)

Returns the value of attribute format.



8
9
10
# File 'lib/canon/comparison/comparison_result.rb', line 8

def format
  @format
end

#html_versionObject (readonly)

Returns the value of attribute html_version.



8
9
10
# File 'lib/canon/comparison/comparison_result.rb', line 8

def html_version
  @html_version
end

#match_optionsObject (readonly)

Returns the value of attribute match_options.



8
9
10
# File 'lib/canon/comparison/comparison_result.rb', line 8

def match_options
  @match_options
end

#original_stringsObject (readonly)

Returns the value of attribute original_strings.



8
9
10
# File 'lib/canon/comparison/comparison_result.rb', line 8

def original_strings
  @original_strings
end

#preprocessed_stringsObject (readonly)

Returns the value of attribute preprocessed_strings.



8
9
10
# File 'lib/canon/comparison/comparison_result.rb', line 8

def preprocessed_strings
  @preprocessed_strings
end

Instance Method Details

#diff(use_color: true, context_lines: 3, diff_grouping_lines: nil, show_diffs: :all, diff_mode: :separate, legacy_terminal: false) ⇒ String

Generate formatted diff output

Parameters:

  • use_color (Boolean) (defaults to: true)

    Whether to use ANSI color codes

  • context_lines (Integer) (defaults to: 3)

    Number of context lines to show

  • diff_grouping_lines (Integer) (defaults to: nil)

    Maximum gap for grouping diffs

  • show_diffs (Symbol) (defaults to: :all)

    Which diffs to show (:all, :normative, :informative)

  • diff_mode (Symbol) (defaults to: :separate)

    Diff display mode (:separate, :inline)

  • legacy_terminal (Boolean) (defaults to: false)

    Force legacy mode (no ANSI, separate-line only)

Returns:

  • (String)

    Formatted diff output



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/canon/comparison/comparison_result.rb', line 96

def diff(use_color: true, context_lines: 3, diff_grouping_lines: nil,
show_diffs: :all, diff_mode: :separate, legacy_terminal: false)
  require_relative "../diff_formatter"

  formatter = Canon::DiffFormatter.new(
    use_color: use_color,
    mode: :by_line,
    context_lines: context_lines,
    diff_grouping_lines: diff_grouping_lines,
    show_diffs: show_diffs,
    diff_mode: diff_mode,
    legacy_terminal: legacy_terminal,
  )

  # Pass self (ComparisonResult) so formatter can check equivalent? status
  formatter.format(
    self,
    @format,
    doc1: @preprocessed_strings[0],
    doc2: @preprocessed_strings[1],
    html_version: @html_version,
  )
end

#equivalent?Boolean

Check if documents are semantically equivalent (no normative diffs)

Returns:

  • (Boolean)

    true if no normative differences present



32
33
34
# File 'lib/canon/comparison/comparison_result.rb', line 32

def equivalent?
  !has_normative_diffs?
end

#has_informative_diffs?Boolean

Check if there are any informative (textual-only) differences

Returns:

  • (Boolean)

    true if at least one informative diff exists



56
57
58
59
60
# File 'lib/canon/comparison/comparison_result.rb', line 56

def has_informative_diffs?
  @differences.any? do |diff|
    diff.is_a?(Canon::Diff::DiffNode) && diff.informative?
  end
end

#has_normative_diffs?Boolean

Check if there are any normative (semantic) differences Includes both DiffNode objects marked as normative AND legacy Hash differences (which represent structural differences like element name mismatches)

Returns:

  • (Boolean)

    true if at least one normative diff exists



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/canon/comparison/comparison_result.rb', line 41

def has_normative_diffs?
  @differences.any? do |diff|
    # DiffNode objects - check if marked normative
    if diff.is_a?(Canon::Diff::DiffNode)
      diff.normative?
    # Legacy Hash format - always considered normative (structural differences)
    else
      diff.is_a?(Hash)
    end
  end
end

#informative_differencesArray<DiffNode>

Get all informative differences

Returns:

  • (Array<DiffNode>)

    Informative differences only



74
75
76
77
78
# File 'lib/canon/comparison/comparison_result.rb', line 74

def informative_differences
  @differences.select do |diff|
    diff.is_a?(Canon::Diff::DiffNode) && diff.informative?
  end
end

#normative_differencesArray<DiffNode>

Get all normative differences

Returns:

  • (Array<DiffNode>)

    Normative differences only



65
66
67
68
69
# File 'lib/canon/comparison/comparison_result.rb', line 65

def normative_differences
  @differences.select do |diff|
    diff.is_a?(Canon::Diff::DiffNode) && diff.normative?
  end
end

#operationsArray<Operation>

Get tree diff operations (only available when diff_algorithm: :semantic)

Returns:

  • (Array<Operation>)

    Array of tree diff operations



83
84
85
# File 'lib/canon/comparison/comparison_result.rb', line 83

def operations
  @match_options&.[](:tree_diff_operations) || []
end