Sha256: 1875cd30bd9b867738b6be5a20d1fb5a3cddf9d8f8bfa52c42b725a1c76f1d31

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

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

    def initialize(collection, options = {})
      @collection = Array(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

    private

    def generate_file(options = {})
      CSV.generate(**options) do |file|
        file << headers
        collection.each do |object|
          file << exporter_for(object).values
        end
      end
    end

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

    def headers
      object = collection.first
      attributes = exporter_for(object).attributes

      if object.class.respond_to?(:human_attribute_name)
        attributes.map { |attr| object.class.human_attribute_name(attr) }
      else
        attributes.map(&:to_s)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_model_exporters-0.7.0 lib/active_model/array_exporter.rb
active_model_exporters-0.6.0 lib/active_model/array_exporter.rb