Method: Rspreadsheet::Workbook#save

Defined in:
lib/rspreadsheet/workbook.rb

#save(new_filename = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rspreadsheet/workbook.rb', line 19

def save(new_filename=nil)
  if @filename.nil? and new_filename.nil? then raise 'New file should be named on first save.' end
  # if the filename has changed than first copy the original file to new location (or template if it is a new file)
  if new_filename
    FileUtils.cp(@filename || File.dirname(__FILE__)+'/empty_file_template.ods', new_filename)
    @filename = new_filename
  end
  Zip::File.open(@filename) do |zip|
    # it is easy, because @xmlnode in in sync with contents all the time
    zip.get_output_stream('content.xml') do |f|
      f.write @content_xml.to_s(:indent => false)
    end
  end
end