class Decisive::XLSContext

Public Instance Methods

csv?() click to toggle source
# File lib/decisive/template_handler.rb, line 141
def csv?
  false
end
to_xls() click to toggle source
# File lib/decisive/template_handler.rb, line 137
def to_xls
  to_string(render(Spreadsheet::Workbook.new))
end

Private Instance Methods

render(xls) click to toggle source
# File lib/decisive/template_handler.rb, line 147
def render xls
  worksheets.each do |name, enumerable|
    sheet = xls.create_worksheet(name: sanitize_name(name))

    rows = to_array(enumerable)

    rows.each.with_index do |row, index|
      sheet.row(index).concat row
    end
  end
  xls
end
sanitize_name(name) click to toggle source
# File lib/decisive/template_handler.rb, line 160
def sanitize_name name
  name
    .gsub(/[\[\]\*\?:\/\\\t\n\r]/, " ")
    .gsub(/^'/, "")
    .gsub(/'$/, "")
    .strip
    .slice(0,31)
end
to_array(records) click to toggle source
# File lib/decisive/template_handler.rb, line 169
def to_array records
  context = RenderContext.new(records, nil, block)
  context.send(:header) + context.send(:body)
end
to_string(xls) click to toggle source
# File lib/decisive/template_handler.rb, line 174
def to_string xls
  io = StringIO.new
  xls.write(io)
  io.rewind
  string = io.read
  string.force_encoding(Encoding::ASCII_8BIT)
  string
end