Class: Canon::Comparison::DiffNodeBuilder
- Inherits:
-
Object
- Object
- Canon::Comparison::DiffNodeBuilder
- Defined in:
- lib/canon/comparison/xml_comparator/diff_node_builder.rb
Overview
Builder for creating enriched DiffNode objects Handles path building, serialization, and attribute extraction
Class Method Summary collapse
-
.build(node1:, node2:, diff1:, diff2:, dimension:, **_opts) ⇒ DiffNode?
Build an enriched DiffNode.
-
.build_attribute_difference_reason(attrs1, attrs2) ⇒ String
Build a clear reason message for attribute presence differences Shows which attributes are only in node1, only in node2, or different values.
-
.build_path(node) ⇒ String?
Build canonical path for a node.
-
.build_reason(node1, node2, diff1, diff2, dimension) ⇒ String
Build a human-readable reason for a difference.
-
.build_text_difference_reason(text1, text2) ⇒ String
Build a clear reason message for text content differences Shows the actual text content (truncated if too long).
-
.enrich_metadata(node1, node2) ⇒ Hash
Enrich DiffNode with canonical path, serialized content, and attributes This extracts presentation-ready metadata from nodes for Stage 4 rendering.
-
.extract_attributes(node) ⇒ Hash?
Extract attributes from a node as a normalized hash.
-
.extract_text_content(node) ⇒ String?
Extract text content from a node.
-
.serialize(node) ⇒ String?
Serialize a node to string for display.
-
.truncate(text, max_length = 40) ⇒ String
Truncate text for display in reason messages.
Class Method Details
.build(node1:, node2:, diff1:, diff2:, dimension:, **_opts) ⇒ DiffNode?
Build an enriched DiffNode
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 21 def self.build(node1:, node2:, diff1:, diff2:, dimension:, **_opts) # Validate dimension is required if dimension.nil? raise ArgumentError, "dimension required for DiffNode" end # Build informative reason message reason = build_reason(node1, node2, diff1, diff2, dimension) # Enrich with path, serialized content, and attributes for Stage 4 rendering = (node1, node2) Canon::Diff::DiffNode.new( node1: node1, node2: node2, dimension: dimension, reason: reason, **, ) end |
.build_attribute_difference_reason(attrs1, attrs2) ⇒ String
Build a clear reason message for attribute presence differences Shows which attributes are only in node1, only in node2, or different values
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 143 def self.build_attribute_difference_reason(attrs1, attrs2) return "#{attrs1&.keys&.size || 0} vs #{attrs2&.keys&.size || 0} attributes" unless attrs1 && attrs2 keys1 = attrs1.keys.to_set keys2 = attrs2.keys.to_set only_in_1 = keys1 - keys2 only_in_2 = keys2 - keys1 common = keys1 & keys2 # Check if values differ for common keys different_values = common.reject { |k| attrs1[k] == attrs2[k] } parts = [] parts << "only in first: #{only_in_1.to_a.sort.join(', ')}" if only_in_1.any? parts << "only in second: #{only_in_2.to_a.sort.join(', ')}" if only_in_2.any? parts << "different values: #{different_values.sort.join(', ')}" if different_values.any? if parts.empty? "#{keys1.size} vs #{keys2.size} attributes (same names)" else parts.join("; ") end end |
.build_path(node) ⇒ String?
Build canonical path for a node
111 112 113 114 115 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 111 def self.build_path(node) return nil if node.nil? Canon::Diff::PathBuilder.build(node, format: :document) end |
.build_reason(node1, node2, diff1, diff2, dimension) ⇒ String
Build a human-readable reason for a difference
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 51 def self.build_reason(node1, node2, diff1, diff2, dimension) # For deleted/inserted nodes, include namespace information if available if dimension == :text_content && (node1.nil? || node2.nil?) node = node1 || node2 if node.respond_to?(:name) && node.respond_to?(:namespace_uri) ns = node.namespace_uri ns_info = if ns.nil? || ns.empty? "" else " (namespace: #{ns})" end return "element '#{node.name}'#{ns_info}: #{diff1} vs #{diff2}" end end # For attribute presence differences, show what attributes differ if dimension == :attribute_presence attrs1 = extract_attributes(node1) attrs2 = extract_attributes(node2) return build_attribute_difference_reason(attrs1, attrs2) end # For text content differences, show the actual text (truncated if needed) if dimension == :text_content text1 = extract_text_content(node1) text2 = extract_text_content(node2) return build_text_difference_reason(text1, text2) end # For attribute order differences, show the actual attribute names if dimension == :attribute_order attrs1 = extract_attributes(node1)&.keys || [] attrs2 = extract_attributes(node2)&.keys || [] return "Attribute order changed: [#{attrs1.join(', ')}] → [#{attrs2.join(', ')}]" end # Default reason "#{diff1} vs #{diff2}" end |
.build_text_difference_reason(text1, text2) ⇒ String
Build a clear reason message for text content differences Shows the actual text content (truncated if too long)
205 206 207 208 209 210 211 212 213 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 205 def self.build_text_difference_reason(text1, text2) # Handle nil cases return "missing vs '#{truncate(text2)}'" if text1.nil? && text2 return "'#{truncate(text1)}' vs missing" if text1 && text2.nil? return "both missing" if text1.nil? && text2.nil? # Both have content - show truncated versions "'#{truncate(text1)}' vs '#{truncate(text2)}'" end |
.enrich_metadata(node1, node2) ⇒ Hash
Enrich DiffNode with canonical path, serialized content, and attributes This extracts presentation-ready metadata from nodes for Stage 4 rendering
97 98 99 100 101 102 103 104 105 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 97 def self.(node1, node2) { path: build_path(node1 || node2), serialized_before: serialize(node1), serialized_after: serialize(node2), attributes_before: extract_attributes(node1), attributes_after: extract_attributes(node2), } end |
.extract_attributes(node) ⇒ Hash?
Extract attributes from a node as a normalized hash
131 132 133 134 135 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 131 def self.extract_attributes(node) return nil if node.nil? Canon::Diff::NodeSerializer.extract_attributes(node) end |
.extract_text_content(node) ⇒ String?
Extract text content from a node
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 172 def self.extract_text_content(node) return nil if node.nil? # For Canon::Xml::Nodes::TextNode return node.value if node.respond_to?(:value) && node.is_a?(Canon::Xml::Nodes::TextNode) # For XML/HTML nodes with text_content method return node.text_content if node.respond_to?(:text_content) # For nodes with text method return node.text if node.respond_to?(:text) # For nodes with content method (Moxml::Text) return node.content if node.respond_to?(:content) # For nodes with value method (other types) return node.value if node.respond_to?(:value) # For simple text nodes or strings return node.to_s if node.is_a?(String) # For other node types, try to_s node.to_s rescue StandardError nil end |
.serialize(node) ⇒ String?
Serialize a node to string for display
121 122 123 124 125 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 121 def self.serialize(node) return nil if node.nil? Canon::Diff::NodeSerializer.serialize(node) end |
.truncate(text, max_length = 40) ⇒ String
Truncate text for display in reason messages
220 221 222 223 224 225 226 227 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 220 def self.truncate(text, max_length = 40) return "" if text.nil? text = text.to_s return text if text.length <= max_length "#{text[0...max_length]}..." end |