Class: Canon::Comparison::ComparisonResult
- Inherits:
-
Object
- Object
- Canon::Comparison::ComparisonResult
- 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
-
#algorithm ⇒ Object
readonly
Returns the value of attribute algorithm.
-
#differences ⇒ Object
readonly
Returns the value of attribute differences.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#html_version ⇒ Object
readonly
Returns the value of attribute html_version.
-
#match_options ⇒ Object
readonly
Returns the value of attribute match_options.
-
#original_strings ⇒ Object
readonly
Returns the value of attribute original_strings.
-
#preprocessed_strings ⇒ Object
readonly
Returns the value of attribute preprocessed_strings.
Instance Method Summary collapse
-
#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.
-
#equivalent? ⇒ Boolean
Check if documents are semantically equivalent (no normative diffs).
-
#has_informative_diffs? ⇒ Boolean
Check if there are any informative (textual-only) differences.
-
#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).
-
#informative_differences ⇒ Array<DiffNode>
Get all informative differences.
-
#initialize(differences:, preprocessed_strings:, format:, html_version: nil, match_options: nil, algorithm: :dom, original_strings: nil) ⇒ ComparisonResult
constructor
A new instance of ComparisonResult.
-
#normative_differences ⇒ Array<DiffNode>
Get all normative differences.
-
#operations ⇒ Array<Operation>
Get tree diff operations (only available when diff_algorithm: :semantic).
-
#summary ⇒ String
Generate a human-readable summary of the first difference.
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.
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 = @algorithm = algorithm end |
Instance Attribute Details
#algorithm ⇒ Object (readonly)
Returns the value of attribute algorithm.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def algorithm @algorithm end |
#differences ⇒ Object (readonly)
Returns the value of attribute differences.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def differences @differences end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def format @format end |
#html_version ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute match_options.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def @match_options end |
#original_strings ⇒ Object (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_strings ⇒ Object (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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/canon/comparison/comparison_result.rb', line 120 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)
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
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)
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_differences ⇒ Array<DiffNode>
Get all informative differences
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_differences ⇒ Array<DiffNode>
Get all normative differences
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 |
#operations ⇒ Array<Operation>
Get tree diff operations (only available when diff_algorithm: :semantic)
83 84 85 |
# File 'lib/canon/comparison/comparison_result.rb', line 83 def operations @match_options&.[](:tree_diff_operations) || [] end |
#summary ⇒ String
Generate a human-readable summary of the first difference.
When documents are equivalent, returns “Equivalent”. When they differ, returns a single-line string with the first normative (or first informative) difference location and reason.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/canon/comparison/comparison_result.rb', line 94 def summary return "Equivalent" if equivalent? diff = normative_differences.first || informative_differences.first || @differences.first # rubocop:disable Layout/MultilineOperationIndentation return "Not equivalent" unless diff if diff.is_a?(Canon::Diff::DiffNode) summarize_diff_node(diff) elsif diff.is_a?(Hash) summarize_legacy_hash(diff) else "Not equivalent" end end |