Class: Canon::Comparison::ProfileDefinition

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

Overview

Profile definition DSL with full validation

Provides a clean, validated way to define custom comparison profiles. Catches errors at definition time with clear, actionable messages.

Examples:

Define a custom profile

Canon::Comparison.define_profile(:my_custom) do
  text_content :normalize
  comments :ignore
  preprocessing :rendered
end

Constant Summary collapse

VALID_DIMENSIONS =

All valid dimensions for XML/HTML comparison These must match MatchOptions::Xml::MATCH_DIMENSIONS

%i[
  text_content
  structural_whitespace
  attribute_presence
  attribute_order
  attribute_values
  element_position
  comments
].freeze
DIMENSION_BEHAVIORS =

Behaviors valid for each dimension Maps dimension name to array of valid behavior symbols

{
  text_content: %i[strict normalize ignore],
  structural_whitespace: %i[strict normalize ignore],
  attribute_presence: %i[strict ignore],
  attribute_order: %i[strict ignore],
  attribute_values: %i[strict strip compact normalize ignore],
  element_position: %i[strict ignore],
  comments: %i[strict ignore],
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ProfileDefinition

Initialize a new profile definition

Parameters:

  • name (Symbol)

    Profile name



46
47
48
49
# File 'lib/canon/comparison/profile_definition.rb', line 46

def initialize(name)
  @name = name
  @settings = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



41
42
43
# File 'lib/canon/comparison/profile_definition.rb', line 41

def name
  @name
end

#settingsObject (readonly)

Returns the value of attribute settings.



41
42
43
# File 'lib/canon/comparison/profile_definition.rb', line 41

def settings
  @settings
end

Class Method Details

.define(name) {|ProfileDefinition| ... } ⇒ Hash

Define a profile using DSL syntax

Parameters:

  • name (Symbol)

    Profile name

Yields:

Returns:

  • (Hash)

    Profile settings hash

Raises:



57
58
59
60
61
62
# File 'lib/canon/comparison/profile_definition.rb', line 57

def self.define(name, &block)
  definition = new(name)
  definition.instance_eval(&block) if block
  definition.validate!
  definition.to_h
end

Instance Method Details

#preprocessing(mode) ⇒ Object

Set preprocessing mode

Parameters:

  • mode (Symbol)

    Preprocessing mode

Raises:



75
76
77
78
79
80
81
82
83
# File 'lib/canon/comparison/profile_definition.rb', line 75

def preprocessing(mode)
  unless MatchOptions::PREPROCESSING_OPTIONS.include?(mode)
    raise ProfileError,
          "Invalid preprocessing mode: #{mode}. " \
          "Valid options: #{MatchOptions::PREPROCESSING_OPTIONS.join(', ')}"
  end

  @settings[:preprocessing] = mode
end

#semantic_diff(enabled: true) ⇒ Object

Enable/disable semantic diff

Parameters:

  • enabled (Boolean) (defaults to: true)

    Whether to enable semantic diff (default: true)



88
89
90
# File 'lib/canon/comparison/profile_definition.rb', line 88

def semantic_diff(enabled: true)
  @settings[:semantic_diff] = enabled
end

#similarity_threshold(value) ⇒ Object

Set similarity threshold for semantic matching

Parameters:

  • value (Numeric)

    Threshold between 0 and 1

Raises:



96
97
98
99
100
101
102
103
# File 'lib/canon/comparison/profile_definition.rb', line 96

def similarity_threshold(value)
  unless value.is_a?(Numeric) && value >= 0 && value <= 1
    raise ProfileError,
          "Similarity threshold must be between 0 and 1, got: #{value}"
  end

  @settings[:similarity_threshold] = value
end

#to_hHash

Convert to hash

Returns:

  • (Hash)

    Profile settings



117
118
119
# File 'lib/canon/comparison/profile_definition.rb', line 117

def to_h
  @settings.dup
end

#validate!Object

Validate the profile definition

Raises:



108
109
110
111
112
# File 'lib/canon/comparison/profile_definition.rb', line 108

def validate!
  @settings.each do |key, value|
    validate_dimension!(key, value) if VALID_DIMENSIONS.include?(key)
  end
end