class Decisive::XLSWithWorksheetsContext

Public Class Methods

new(*args, &block) click to toggle source
Calls superclass method
# File lib/decisive/template_handler.rb, line 79
def initialize *args, &block
  super
  instance_eval &block
end

Public Instance Methods

csv?() click to toggle source
# File lib/decisive/template_handler.rb, line 88
def csv?
  false
end
to_xls() click to toggle source
# File lib/decisive/template_handler.rb, line 84
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 98
def render xls
  worksheets.each do |worksheet|
    sheet = xls.create_worksheet(name: sanitize_name(worksheet.name))

    rows = to_array(worksheet)

    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 111
def sanitize_name name
  name
    .gsub(/[\[\]\*\?:\/\\\t\n\r]/, " ")
    .gsub(/^'/, "")
    .gsub(/'$/, "")
    .strip
    .slice(0,31)
end
to_array(worksheet) click to toggle source
# File lib/decisive/template_handler.rb, line 120
def to_array worksheet
  context = RenderContext.new(worksheet.records, nil, worksheet.block)
  context.send(:header) + context.send(:body)
end
to_string(xls) click to toggle source
# File lib/decisive/template_handler.rb, line 125
def to_string xls
  io = StringIO.new
  xls.write(io)
  io.rewind
  string = io.read
  string.force_encoding(Encoding::ASCII_8BIT)
  string
end
worksheet(records, name:, &block) click to toggle source
# File lib/decisive/template_handler.rb, line 94
def worksheet records, name:, &block
  worksheets.push Worksheet.new(records, name, block)
end