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
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 136 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
104 105 106 107 108 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 104 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 |
# 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 # 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)
198 199 200 201 202 203 204 205 206 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 198 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
90 91 92 93 94 95 96 97 98 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 90 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
124 125 126 127 128 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 124 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
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 165 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
114 115 116 117 118 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 114 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
213 214 215 216 217 218 219 220 |
# File 'lib/canon/comparison/xml_comparator/diff_node_builder.rb', line 213 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 |