Class: Canon::Comparison::Strategies::BaseMatchStrategy

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

Overview

Abstract base class for match strategies

All match strategies must inherit from this class and implement:

  • match(doc1, doc2) → Array<DiffNode>

  • preprocess_for_display(doc1, doc2) → [String, String]

This provides a common interface for different matching algorithms, enabling the Strategy Pattern for extensible comparison methods.

Examples:

Create a custom match strategy

class MyMatchStrategy < BaseMatchStrategy
  def match(doc1, doc2)
    # Custom matching logic
    # Must return Array<Canon::Diff::DiffNode>
  end

  def preprocess_for_display(doc1, doc2)
    # Format documents for diff display
    # Must return [String, String]
  end
end

Direct Known Subclasses

SemanticTreeMatchStrategy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format:, match_options:) ⇒ BaseMatchStrategy

Initialize strategy

Parameters:

  • format (Symbol)

    Document format (:xml, :html, :json, :yaml)

  • match_options (Hash)

    Match options for comparison



35
36
37
38
# File 'lib/canon/comparison/strategies/base_match_strategy.rb', line 35

def initialize(format:, match_options:)
  @format = format
  @match_options = match_options
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



29
30
31
# File 'lib/canon/comparison/strategies/base_match_strategy.rb', line 29

def format
  @format
end

#match_optionsObject (readonly)

Returns the value of attribute match_options.



29
30
31
# File 'lib/canon/comparison/strategies/base_match_strategy.rb', line 29

def match_options
  @match_options
end

Instance Method Details

#algorithm_nameSymbol

Algorithm name derived from class name

Automatically generates algorithm identifier from class name. For example:

  • DomMatchStrategy → :dom

  • SemanticTreeMatchStrategy → :semantic_tree

Returns:

  • (Symbol)

    Algorithm identifier



89
90
91
92
93
94
95
# File 'lib/canon/comparison/strategies/base_match_strategy.rb', line 89

def algorithm_name
  self.class.name.split("::").last
    .gsub("MatchStrategy", "")
    .gsub(/([A-Z])/, '_\1')
    .downcase[1..]
    .to_sym
end

#match(doc1, doc2) ⇒ Array<Canon::Diff::DiffNode>

Perform matching and return DiffNodes

This is the core method that implements the matching algorithm. All strategies must implement this to produce DiffNodes that flow through the standard diff rendering pipeline.

Parameters:

  • doc1 (Object)

    First document

  • doc2 (Object)

    Second document

Returns:

Raises:

  • (NotImplementedError)

    If not implemented by subclass



50
51
52
53
# File 'lib/canon/comparison/strategies/base_match_strategy.rb', line 50

def match(doc1, doc2)
  raise NotImplementedError,
        "#{self.class} must implement #match(doc1, doc2)"
end

#metadataHash

Optional metadata to include in ComparisonResult

Subclasses can override this to provide algorithm-specific metadata such as statistics, configuration, etc.

Returns:

  • (Hash)

    Additional metadata



77
78
79
# File 'lib/canon/comparison/strategies/base_match_strategy.rb', line 77

def 
  {}
end

#preprocess_for_display(doc1, doc2) ⇒ Array<String>

Preprocess documents for display in diff output

This method formats the documents into strings suitable for line-by-line diff display. The format must be consistent across all strategies for the same format to ensure the diff rendering pipeline produces correct output.

Parameters:

  • doc1 (Object)

    First document

  • doc2 (Object)

    Second document

Returns:

  • (Array<String>)

    Preprocessed [doc1_string, doc2_string]

Raises:

  • (NotImplementedError)

    If not implemented by subclass



66
67
68
69
# File 'lib/canon/comparison/strategies/base_match_strategy.rb', line 66

def preprocess_for_display(doc1, doc2)
  raise NotImplementedError,
        "#{self.class} must implement #preprocess_for_display(doc1, doc2)"
end