Sha256: 491a4a3a4a8ac1ce0c79abb3a0b9e3d8e91f57c38fc4d21b68a8b27b66235fc2

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require "action_dispatch/http/mime_type"
require "spreadsheet"

class ColumnifyTemplate
  class << self
    attr_accessor :template_lookup_options
  end

  self.template_lookup_options = {handlers: [:columnify]}

  def initialize(context, *args)
    @context = context
    super(*args)
  end

  def workbook
    @workbook ||= Spreadsheet::Workbook.new
  end

  def worksheet(resources, *args)
    sheet = workbook.create_worksheet(name: worksheet_name.to_s)

    sheet.row(0).concat args.map(&:to_s).map(&:humanize)

    resources.each_with_index do |resource, index|
      args.each do |method_name|
        sheet.row(index + 1).push(resource.send(method_name))
      end
    end

    buffer = StringIO.new
    workbook.write(buffer)
    buffer.rewind
    buffer.read
  end

  private

  def worksheet_name
    DateTime.now.to_i
  end
end

class ColumnifyHandler
  cattr_accessor :default_format
  self.default_format = Columnify::Mime::XLS

  def self.call(template, source = nil)
    source ||= template.source
    # this juggling is required to keep line numbers right in the error
    %{__already_defined = defined?(spreadsheet); spreadsheet||=ColumnifyTemplate.new(self); #{source}}
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
columnify-0.1.0 lib/columnify/columnify_template.rb