Sha256: 876c585cc351e5c5e5f6cb8add24e4df08e6f8091c41af6d38c698cf9f181679

Contents?: true

Size: 725 Bytes

Versions: 1

Compression:

Stored size: 725 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

    def generate_file(options = {})
      CSV.generate(options) do |file|
        collection.each do |object|
          file << exporter_for(object).values
        end
      end
    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.5 lib/active_model/array_exporter.rb