Sha256: 40120257794363741b5462da3f365fc66aec47e69a91091454adc52a1a238977

Contents?: true

Size: 879 Bytes

Versions: 2

Compression:

Stored size: 879 Bytes

Contents

class ActiveReport::Array

  attr_accessor :datum, :headers, :options

  def initialize(datum, headers: nil, options: {})
    @datum   = datum
    @headers = headers
    @options = options
  end

  def self.export(datum, headers: nil, options: {})
    new(datum, headers: headers, options: options).export
  end

  def export
    @datum = [].push(@datum).compact unless @datum.first.is_a?(Array)

    CSV.generate(@options) do |csv|
      csv << @headers unless @headers.nil?
      @datum.each { |data| csv << data }
    end
  end

  def self.import(datum, headers: nil, options: {})
    new(datum, headers: headers, options: options).import
  end

  def import
    processed_datum = [].push(@headers).compact
    CSV.foreach(@datum, @options) do |data|
      processed_datum.push(data)
    end
    processed_datum.count < 2 ? processed_datum.flatten : processed_datum
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_report-1.0.0 lib/active_report/array.rb
active_report-0.0.1 lib/active_report/array.rb