Module: Canon::Comparison::MatchOptions

Defined in:
lib/canon/comparison/match_options.rb,
lib/canon/comparison/match_options/xml_resolver.rb,
lib/canon/comparison/match_options/base_resolver.rb,
lib/canon/comparison/match_options/json_resolver.rb,
lib/canon/comparison/match_options/yaml_resolver.rb

Overview

Module containing match option utilities and format-specific modules

Defined Under Namespace

Modules: Json, Xml, Yaml Classes: BaseResolver, JsonResolver, XmlResolver, YamlResolver

Constant Summary collapse

PREPROCESSING_OPTIONS =

Preprocessing options - what to do before comparison

%i[none c14n normalize format rendered].freeze
MATCH_BEHAVIORS =

Matching behaviors (deprecated - use per-dimension validation instead) This universal constant is kept for backward compatibility but should not be used for validation. Use BaseResolver.dimension_behaviors instead. Note: :strip and :compact are only valid for attribute_values dimension.

%i[strict strip compact normalize ignore].freeze

Class Method Summary collapse

Class Method Details

.match_text?(text1, text2, behavior) ⇒ Boolean

Apply match behavior to text comparison

Parameters:

  • text1 (String)

    First text

  • text2 (String)

    Second text

  • behavior (Symbol)

    Match behavior (:strict, :normalize, :ignore)

Returns:

  • (Boolean)

    true if texts match according to behavior



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

def match_text?(text1, text2, behavior)
  case behavior
  when :strict
    text1 == text2
  when :normalize
    normalize_text(text1) == normalize_text(text2)
  when :ignore
    true
  else
    raise Canon::Error, "Unknown match behavior: #{behavior}"
  end
end

.normalize_text(text) ⇒ String

Normalize text by collapsing whitespace and trimming Mimics HTML whitespace collapsing

Handles both ASCII and Unicode whitespace characters including:

  • Regular space (U+0020)

  • Non-breaking space (U+00A0)

  • Other Unicode whitespace per pSpace

Parameters:

  • text (String)

    Text to normalize

Returns:

  • (String)

    Normalized text



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

def normalize_text(text)
  return "" if text.nil?

  text.to_s
    .gsub(/[\p{Space}\u00a0]+/, " ") # Collapse all whitespace to single space
    .strip # Remove leading/trailing whitespace
end

.process_attribute_value(value, behavior) ⇒ String

Process attribute value according to match behavior

Parameters:

  • value (String)

    Attribute value to process

  • behavior (Symbol)

    Match behavior (:strict, :strip, :compact, :normalize, :ignore)

Returns:

  • (String)

    Processed value



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/canon/comparison/match_options.rb', line 109

def process_attribute_value(value, behavior)
  case behavior
  when :strict
    value.to_s
  when :strip
    value.to_s.strip
  when :compact
    value.to_s.gsub(/[\p{Space}\u00a0]+/, " ")
  when :normalize
    normalize_text(value)
  when :ignore
    ""
  else
    raise Canon::Error, "Unknown attribute value behavior: #{behavior}"
  end
end