class SimpleReport::Base
Public Class Methods
new()
click to toggle source
# File lib/simple_report/base.rb, line 3 def initialize @sheets = [] end
Public Instance Methods
add_format(name, format)
click to toggle source
# File lib/simple_report/base.rb, line 7 def add_format(name, format) @formats ||= {} @formats[name] = format end
add_formats()
click to toggle source
# File lib/simple_report/base.rb, line 62 def add_formats money = @workbook.add_format money.set_num_format('$0.00') add_format :money, money heading = @workbook.add_format heading.set_bold add_format :heading, heading percent = @workbook.add_format percent.set_num_format('0.0%') add_format :percent, percent end
add_sheet(name, data) { |sheet| ... }
click to toggle source
# File lib/simple_report/base.rb, line 19 def add_sheet(name, data) sheet = Sheet.new(name, data) yield sheet @sheets ||= [] @sheets << sheet end
build_report()
click to toggle source
# File lib/simple_report/base.rb, line 76 def build_report raise NotImplementedError.new('<report>#build_report not implemented') end
find_format(format)
click to toggle source
# File lib/simple_report/base.rb, line 58 def find_format(format) @formats[format] end
generate_report()
click to toggle source
# File lib/simple_report/base.rb, line 26 def generate_report @file = Tempfile.new('simple_report') @workbook = WriteXLSX.new(@file.path) add_formats @sheets.each do |sheet| output_sheet = @workbook.add_worksheet(sheet.name) sheet.fields.each_with_index do |f, index| output_sheet.set_column(index, index, f.width) output_sheet.write(0, index, f.name, @formats[:heading]) end sheet.collection.each_with_index do |ic, record_num| sheet.fields.each_with_index do |field, column| if field.field value = ic.send(field.field) elsif field.value value = field.value elsif field.block value = field.block.call(ic, record_num + 2) end case field.force when nil output_sheet.write(record_num + 1, column, value, find_format(field.format)) when :string output_sheet.write_string(record_num + 1, column, value, find_format(field.format)) else raise "invalid force param" end end end end end
to_xlsx(*params)
click to toggle source
# File lib/simple_report/base.rb, line 12 def to_xlsx(*params) build_report(*params) generate_report @workbook.close File.read @file.path end