Sha256: 0d81217f060413de2ad3e93496fcb2ffc4f0399110f2a3b7f105a8fe6921bf88

Contents?: true

Size: 804 Bytes

Versions: 1

Compression:

Stored size: 804 Bytes

Contents

module ActiveModel
  class ArrayExporter
    attr_reader :collection, :exporter, :scope

    def initialize(collection, options = {})
      @collection = collection
      @scope      = options.delete(:scope)
      @exporter   = options.delete(:exporter)
    end

    def to_csv
      generate_file
    end

    def to_xls
      generate_file(col_sep: "\t")
    end

    alias :to_xlsx :to_xls

    def generate_file(options = {})
      CSV.generate(options) do |file|
        collection.each do |object|
          exporter = exporter_for(object)
          file << exporter.values
        end
      end.encode('ISO-8859-1')
    end

    private

    def exporter_for(object)
      exporter_class = exporter || Exporter.exporter_for(object)
      exporter_class.new(object, scope: scope)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_model_exporters-0.0.4 lib/active_model/array_exporter.rb