Class: Canon::Comparison::Dimensions::AttributeValuesDimension

Inherits:
BaseDimension
  • Object
show all
Defined in:
lib/canon/comparison/dimensions/attribute_values_dimension.rb

Overview

Attribute values dimension

Handles comparison of attribute values. Supports :strict, :strip, :compact, :normalize, and :ignore behaviors.

Behaviors:

  • :strict - Exact attribute value comparison

  • :strip - Compare with leading/trailing whitespace removed

  • :compact - Compare with internal whitespace collapsed

  • :normalize - Compare with whitespace stripped and collapsed

  • :ignore - Skip attribute value comparison

Constant Summary

Constants inherited from BaseDimension

BaseDimension::IGNORE, BaseDimension::NORMALIZE, BaseDimension::STRICT

Instance Method Summary collapse

Methods inherited from BaseDimension

#dimension_name, #equivalent?, #supports_normalization?

Instance Method Details

#compare(data1, data2, behavior) ⇒ Boolean

Compare with custom behavior

Supports the extended behaviors for attribute values.

Parameters:

  • data1 (Object)

    First data

  • data2 (Object)

    Second data

  • behavior (Symbol)

    Comparison behavior

Returns:

  • (Boolean)

    true if data matches according to behavior



108
109
110
111
112
113
114
115
116
117
# File 'lib/canon/comparison/dimensions/attribute_values_dimension.rb', line 108

def compare(data1, data2, behavior)
  case behavior
  when :strip
    compare_strip(data1, data2)
  when :compact
    compare_compact(data1, data2)
  else
    super
  end
end

#compare_compact(attrs1, attrs2) ⇒ Boolean

Compact comparison

Compare with internal whitespace collapsed.

Parameters:

  • attrs1 (Hash)

    First attributes hash

  • attrs2 (Hash)

    Second attributes hash

Returns:

  • (Boolean)

    true if compacted values are equal



77
78
79
80
81
82
83
# File 'lib/canon/comparison/dimensions/attribute_values_dimension.rb', line 77

def compare_compact(attrs1, attrs2)
  all_keys = (attrs1.keys | attrs2.keys)

  all_keys.all? do |key|
    compact_whitespace(attrs1[key].to_s) == compact_whitespace(attrs2[key].to_s)
  end
end

#compare_normalize(attrs1, attrs2) ⇒ Boolean

Normalized comparison

Compare with whitespace stripped and collapsed.

Parameters:

  • attrs1 (Hash)

    First attributes hash

  • attrs2 (Hash)

    Second attributes hash

Returns:

  • (Boolean)

    true if normalized values are equal



92
93
94
95
96
97
98
# File 'lib/canon/comparison/dimensions/attribute_values_dimension.rb', line 92

def compare_normalize(attrs1, attrs2)
  all_keys = (attrs1.keys | attrs2.keys)

  all_keys.all? do |key|
    normalize_text(attrs1[key].to_s) == normalize_text(attrs2[key].to_s)
  end
end

#compare_strict(attrs1, attrs2) ⇒ Boolean

Strict attribute value comparison

Parameters:

  • attrs1 (Hash)

    First attributes hash

  • attrs2 (Hash)

    Second attributes hash

Returns:

  • (Boolean)

    true if all attribute values are exactly equal



46
47
48
49
50
51
52
53
# File 'lib/canon/comparison/dimensions/attribute_values_dimension.rb', line 46

def compare_strict(attrs1, attrs2)
  # Get all unique attribute names
  all_keys = (attrs1.keys | attrs2.keys)

  all_keys.all? do |key|
    attrs1[key].to_s == attrs2[key].to_s
  end
end

#compare_strip(attrs1, attrs2) ⇒ Boolean

Strip comparison

Compare with leading/trailing whitespace removed.

Parameters:

  • attrs1 (Hash)

    First attributes hash

  • attrs2 (Hash)

    Second attributes hash

Returns:

  • (Boolean)

    true if stripped values are equal



62
63
64
65
66
67
68
# File 'lib/canon/comparison/dimensions/attribute_values_dimension.rb', line 62

def compare_strip(attrs1, attrs2)
  all_keys = (attrs1.keys | attrs2.keys)

  all_keys.all? do |key|
    attrs1[key].to_s.strip == attrs2[key].to_s.strip
  end
end

#extract_data(node) ⇒ Hash

Extract attribute values from a node

Returns a hash of attribute name to value.

Parameters:

  • node (Moxml::Node, Nokogiri::XML::Node)

    Node to extract from

Returns:

  • (Hash)

    Attribute name to value mapping



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/canon/comparison/dimensions/attribute_values_dimension.rb', line 27

def extract_data(node)
  return {} unless node

  # Handle Moxml nodes
  if node.is_a?(Moxml::Node)
    extract_from_moxml(node)
  # Handle Nokogiri nodes
  elsif node.is_a?(Nokogiri::XML::Node)
    extract_from_nokogiri(node)
  else
    {}
  end
end