class Columnify::Worksheet

Public Class Methods

new(resources, *args) click to toggle source
# File lib/columnify/worksheet.rb, line 5
def initialize(resources, *args)
  @options = args.extract_options!
  @column_names = @options[:column_names].presence || args
  @attributes = args
  @resources = resources
  @buffer = StringIO.new
  @workbook = Spreadsheet::Workbook.new
end

Public Instance Methods

create() click to toggle source
# File lib/columnify/worksheet.rb, line 14
def create
  inject_column_names
  inject_data
  write_workbook_buffer
  read
end

Private Instance Methods

cell_format() click to toggle source
# File lib/columnify/worksheet.rb, line 46
def cell_format
  @cell_format ||= Spreadsheet::Format.new text_wrap: true
end
humanized_columns() click to toggle source
# File lib/columnify/worksheet.rb, line 50
def humanized_columns
  @humanized_columns ||= @column_names.map(&:to_s).map(&:humanize)
end
inject_column_names() click to toggle source
# File lib/columnify/worksheet.rb, line 32
def inject_column_names
  sheet.row(0).concat humanized_columns
end
inject_data() click to toggle source
# File lib/columnify/worksheet.rb, line 36
def inject_data
  @resources.each_with_index do |resource, index|
    sheet.column(index).default_format = cell_format
    @attributes.each do |method_name|
      sheet.row(index + 1).default_format = cell_format
      sheet.row(index + 1).push(resource.send(method_name))
    end
  end
end
read() click to toggle source
# File lib/columnify/worksheet.rb, line 23
def read
  @buffer.rewind
  @buffer.read
end
sheet() click to toggle source
# File lib/columnify/worksheet.rb, line 54
def sheet
  @sheet ||= @workbook.create_worksheet(name: worksheet_name)
end
worksheet_name() click to toggle source
# File lib/columnify/worksheet.rb, line 58
def worksheet_name
  Time.now.to_i.to_s
end
write_workbook_buffer() click to toggle source
# File lib/columnify/worksheet.rb, line 28
def write_workbook_buffer
  @workbook.write(@buffer)
end