Sha256: 413a7bd6a783b4c1e22f2eb4404ffcae3eea39ecf610837c59685a253bf85b0c

Contents?: true

Size: 1.02 KB

Versions: 9

Compression:

Stored size: 1.02 KB

Contents

module Rubyxls
  class Report

    attr_reader :workbooks
    attr_reader :file_name

    def initialize
      @workbooks = []
      @file_name = 'rubyxls'
      build_workbooks!
    end

    def download!
      if @workbooks.size == 1
        @workbooks.first.to_stream
      else
        stream = StringIO.new
        zipfile = Zip::File.new(stream, true, true)
        @workbooks.each do |workbook|
          zipfile.get_output_stream(workbook.filename) { |zipstream| IO.copy_stream(workbook.to_stream, zipstream) }
        end
        zipfile.write_buffer(stream)
        zipfile.glob('*', &:clean_up)
        stream.rewind
        stream
      end
    end

    def file_extension
      @workbooks.size > 1 ? :zip : :xlsx
    end

    def content_type
      if file_extension == :xlsx
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
      elsif file_extension == :zip
        'application/zip'
      end
    end

    private

    def build_workbooks!
      @workbooks << Rubyxls::Workbook.new
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubyxls-1.2.1 lib/rubyxls/report.rb
rubyxls-1.2.0 lib/rubyxls/report.rb
rubyxls-1.1.0 lib/rubyxls/report.rb
rubyxls-1.0.5 lib/rubyxls/report.rb
rubyxls-1.0.4 lib/rubyxls/report.rb
rubyxls-1.0.3 lib/rubyxls/report.rb
rubyxls-1.0.2 lib/rubyxls/report.rb
rubyxls-1.0.1 lib/rubyxls/report.rb
rubyxls-1.0.0 lib/rubyxls/report.rb