class Canon::TreeDiff::Core::AttributeComparator
comparator.equal?(attrs1, attrs2) # => true
attrs2 = {id: “_”, class: “TOC”}
attrs1 = {class: “TOC”, id: “_”}
comparator = AttributeComparator.new(attribute_order: :ignore)
@example
- Support both strict and normalized comparison modes
- Provide hash-based equality for matching algorithms
- Compare attributes with configurable order sensitivity
Key responsibilities:
in a way that respects match options, particularly attribute_order.
This class encapsulates the logic for comparing node attributes
AttributeComparator provides order-independent attribute comparison
def comparison_hash(attrs)
-
(Hash)- Normalized hash for comparison
Parameters:
-
attrs(Hash) -- Attribute hash
def comparison_hash(attrs) return {} if attrs.nil? || attrs.empty? if attribute_order == :strict attrs else normalize_for_comparison(attrs) end end
def equal?(attrs1, attrs2)
-
(Boolean)- True if attributes are considered equal
Parameters:
-
attrs2(Hash) -- Second attribute hash -
attrs1(Hash) -- First attribute hash
def equal?(attrs1, attrs2) return true if attrs1.nil? && attrs2.nil? return false if attrs1.nil? || attrs2.nil? if attribute_order == :strict attrs1 == attrs2 else normalize_for_comparison(attrs1) == normalize_for_comparison(attrs2) end end
def initialize(attribute_order: :strict)
-
attribute_order(Symbol) -- :strict or :ignore/:normalize
def initialize(attribute_order: :strict) @attribute_order = attribute_order end
def normalize_for_comparison(attrs)
-
(Hash)- Sorted attribute hash
Parameters:
-
attrs(Hash) -- Attribute hash
def normalize_for_comparison(attrs) attrs.sort.to_h end