lib/rspreadsheet/tools.rb in rspreadsheet-0.4.9 vs lib/rspreadsheet/tools.rb in rspreadsheet-0.5.0
- old
+ new
@@ -1,9 +1,13 @@
-module Rspreadsheet
+ require 'pry'
+ module Rspreadsheet
+
# this module contains methods used bz several objects
module Tools
+ using ClassExtensions if RUBY_VERSION > '2.1'
+
def self.only_letters?(x); x.kind_of?(String) and x.match(/^[A-Za-z]*$/) != nil end
def self.kind_of_integer?(x)
(x.kind_of?(Numeric) and x.to_i==x) or
(x.kind_of?(String) and x.match(/^\d*(\.0*)?$/) != nil)
end
@@ -160,11 +164,12 @@
else
node.nil? ? default : Tools.get_ns_attribute(node,ns_prefix,key,nil).andand.value || default
end
end
def self.remove_ns_attribute(node,ns_prefix,key)
- node.attributes.get_attribute_ns(Tools.get_namespace(ns_prefix).href,key)
+ ns = Tools.get_namespace(ns_prefix)
+ attr = node.attributes.get_attribute_ns(ns.href, key)
attr.remove! unless attr.nil?
end
def self.prepare_ns_node(ns_prefix,nodename,value=nil)
LibXML::XML::Node.new(nodename,value, Tools.get_namespace(ns_prefix))
end
@@ -174,11 +179,10 @@
else
node << subnode
end
end
-
def self.get_unused_filename(zip,prefix, extension)
(1000..9999).each do |ndx|
filename = prefix + ndx.to_s + ((Time.now.to_r*1000000000).to_i.to_s(16)) + extension
return filename if zip.find_entry(filename).nil?
end
@@ -186,9 +190,47 @@
end
def self.new_time_value(h,m,s)
Time.new(StartOfEpoch.year,StartOfEpoch.month,StartOfEpoch.day,h,m,s)
end
+
+ def self.output_to_zip_stream(io,&block)
+ if io.kind_of? File or io.kind_of? String
+ Zip::File.open(io, 'br+') do |zip|
+ yield zip
+ end
+ elsif io.kind_of? StringIO # or io.kind_of? IO
+ Zip::OutputStream.write_buffer(io) do |zip|
+ yield zip
+ end
+ end
+ end
+ def self.content_xml_diff(filename1,filename2)
+ content_xml1 = Zip::File.open(filename1) do |zip|
+ LibXML::XML::Document.io zip.get_input_stream('content.xml')
+ end
+ content_xml2 = Zip::File.open(filename2) do |zip|
+ LibXML::XML::Document.io zip.get_input_stream('content.xml')
+ end
+
+ return xml_diff(content_xml1.root,content_xml2.root)
+ end
+
+ def self.xml_file_diff(filename1,filename2)
+ content_xml1 = LibXML::XML::Document.file(filename1).root
+ content_xml2 = LibXML::XML::Document.file(filename2).root
+ return xml_diff(content_xml1, content_xml2)
+ end
+
+ def self.xml_diff(xml_node1,xml_node2)
+ message = []
+ message << xml_node2.first_diff(xml_node1)
+ message << xml_node1.first_diff(xml_node2)
+ message << 'content XML not equal' unless xml_node1.to_s.should == xml_node2.to_s
+ message = message.compact.join('; ')
+ message = nil if message == ''
+ message
+ end
end
end