Class: Canon::Comparison::Strategies::BaseMatchStrategy
- Inherits:
-
Object
- Object
- Canon::Comparison::Strategies::BaseMatchStrategy
- 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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#match_options ⇒ Object
readonly
Returns the value of attribute match_options.
Instance Method Summary collapse
-
#algorithm_name ⇒ Symbol
Algorithm name derived from class name.
-
#initialize(format:, match_options:) ⇒ BaseMatchStrategy
constructor
Initialize strategy.
-
#match(doc1, doc2) ⇒ Array<Canon::Diff::DiffNode>
Perform matching and return DiffNodes.
-
#metadata ⇒ Hash
Optional metadata to include in ComparisonResult.
-
#preprocess_for_display(doc1, doc2) ⇒ Array<String>
Preprocess documents for display in diff output.
Constructor Details
#initialize(format:, match_options:) ⇒ BaseMatchStrategy
Initialize strategy
35 36 37 38 |
# File 'lib/canon/comparison/strategies/base_match_strategy.rb', line 35 def initialize(format:, match_options:) @format = format @match_options = end |
Instance Attribute Details
#format ⇒ Object (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_options ⇒ Object (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 end |
Instance Method Details
#algorithm_name ⇒ Symbol
Algorithm name derived from class name
Automatically generates algorithm identifier from class name. For example:
-
DomMatchStrategy → :dom
-
SemanticTreeMatchStrategy → :semantic_tree
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.
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 |
#metadata ⇒ Hash
Optional metadata to include in ComparisonResult
Subclasses can override this to provide algorithm-specific metadata such as statistics, configuration, etc.
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.
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 |