lib/helpers/class_extensions.rb in rspreadsheet-0.4.9 vs lib/helpers/class_extensions.rb in rspreadsheet-0.5.0
- old
+ new
@@ -12,11 +12,11 @@
end
end
refine LibXML::XML::Node do
def equals?(node2); raise 'nic' end
- def ==(node2)
+ def ===(node2)
self.simplification_of?(node2) and node2.simplification_of?(self)
end
# if node2 contains at least all that I do
def simplification_of?(node2)
first_diff(node2).nil?
@@ -29,19 +29,19 @@
return "#{where}> Names are different: #{self.name} != #{node2.name}" if (self.name != node2.name)
self.attributes.each do |attr|
return "#{where}> Attribute #{attr} have diffent values: #{attr.value} != #{node2.attributes[attr.name]}" unless node2.attributes[attr.name] == attr.value
end
- elems1 = self.elements
- elems2 = node2.elements
- return "#{where}> elements have different number of subelements #{elems1.length} != #{elems2.length}" if (elems1.length != elems2.length)
+ elems1 = self.children
+ elems2 = node2.children
+ return "#{where}> elements have different number of children #{elems1.length} != #{elems2.length}" if (elems1.length != elems2.length)
elems1.each_index do |i|
raise "Nil for i=#{i}" if elems1[i].nil?
if (elems1[i].node_type_name == 'text')
if elems2[i].nil? || (elems1[i].to_s) != (elems2[i].to_s)
- return "#{where}> #{i+1}th text subelements are different: #{elems1[i].to_s} != #{elems2[i].to_s}"
+ return "#{where}> #{i+1}th text children are different: #{elems1[i].to_s} != #{elems2[i].to_s}"
end
elsif (elems1[i].node_type_name == 'element')
unless elems1[i].simplification_of?(elems2[i])
return "#{where}/[#{i+1}]#{elems1[i].first_diff(elems2[i])}"
end
@@ -70,11 +70,11 @@
end
end
end
class LibXML::XML::Node
- def ==(node2)
+ def ===(node2)
self.simplification_of?(node2) and node2.simplification_of?(self)
end
# if node2 contains at least all that I do
def simplification_of?(node2)
first_diff(node2).nil?
@@ -86,19 +86,25 @@
return "#{where}> Equivalent node does not exist: #{self.name} != NOTHING" if node2.nil?
return "#{where}> Names are different: #{self.name} != #{node2.name}" if (self.name != node2.name)
self.attributes.each do |attr|
return "#{where}> Attribute #{attr} have diffent values: #{attr.value} != #{node2.attributes[attr.name]}" unless node2.attributes[attr.name] == attr.value
end
+
+ elems1 = self.children
+ elems2 = node2.children
+ return "#{where}> elements have different number of children #{elems1.length} != #{elems2.length}" if (elems1.length != elems2.length)
- elems1 = self.elements
- elems2 = node2.elements
- # return "#{where}> elements have different number of subelements #{elems1.length} != #{elems2.length}" if (elems1.length != elems2.length)
- elems1.length.times do |i|
- if (elems1[i].node_type_name == 'text') && ((elems1[i].to_s != elems2[i].to_s) )
- return "#{where}> #{i+1}th text subelements are different: #{elems1[i].to_s} != #{elems2[i].to_s}"
- elsif (elems1[i].node_type_name == 'element') && (!elems1[i].simplification_of?(elems2[i]))
- return "#{where}/[#{i+1}]#{elems1[i].first_diff(elems2[i])}"
+ elems1.each_index do |i|
+ raise "Nil for i=#{i}" if elems1[i].nil?
+ if (elems1[i].node_type_name == 'text')
+ if elems2[i].nil? || (elems1[i].to_s) != (elems2[i].to_s)
+ return "#{where}> #{i+1}th text children are different: #{elems1[i].to_s} != #{elems2[i].to_s}"
+ end
+ elsif (elems1[i].node_type_name == 'element')
+ unless elems1[i].simplification_of?(elems2[i])
+ return "#{where}/[#{i+1}]#{elems1[i].first_diff(elems2[i])}"
+ end
end
end
return nil
end
@@ -111,6 +117,6 @@
# def elements
# children.select { |e| e.element? }
# end
end
-end
\ No newline at end of file
+end