Class: Canon::Comparison::Strategies::MatchStrategyFactory

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

Overview

Factory for creating match strategies

Selects the appropriate match strategy based on match options. This provides a single point for strategy instantiation and enables easy extension with new matching algorithms.

Examples:

Create a strategy

strategy = MatchStrategyFactory.create(
  format: :xml,
  match_options: { semantic_diff: true }
)
differences = strategy.match(doc1, doc2)

Class Method Summary collapse

Class Method Details

.create(format:, match_options:) ⇒ BaseMatchStrategy

Create appropriate match strategy

Examines match options to determine which strategy to use:

  • If semantic_diff is enabled: SemanticTreeMatchStrategy

  • Otherwise (default): DomMatchStrategy

Future strategies can be added here by checking additional options and returning the appropriate strategy class.

Examples:

DOM matching (default)

strategy = MatchStrategyFactory.create(
  format: :xml,
  match_options: {}
)
# Returns DomMatchStrategy

Semantic tree matching

strategy = MatchStrategyFactory.create(
  format: :xml,
  match_options: { semantic_diff: true }
)
# Returns SemanticTreeMatchStrategy

Parameters:

  • format (Symbol)

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

  • match_options (Hash)

    Match options

Options Hash (match_options:):

  • :semantic_diff (Boolean)

    Use semantic tree matching

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/canon/comparison/strategies/match_strategy_factory.rb', line 50

def self.create(format:, match_options:)
  # Check for semantic diff option
  if match_options[:semantic_diff]
    require_relative "semantic_tree_match_strategy"
    SemanticTreeMatchStrategy.new(format: format,
                                  match_options: match_options)
  else
    # Default to DOM matching
    require_relative "dom_match_strategy"
    DomMatchStrategy.new(format: format, match_options: match_options)
  end

  # Future: Add more strategies here
  # Example:
  # elsif match_options[:hybrid_diff]
  #   require_relative "hybrid_match_strategy"
  #   HybridMatchStrategy.new(format, match_options)
  # elsif match_options[:fuzzy_diff]
  #   require_relative "fuzzy_match_strategy"
  #   FuzzyMatchStrategy.new(format, match_options)
end