Sha256: 68be76d2a2c4d4827f124266283edb4e6413d77baa0a682243fd2726a4bd4a34

Contents?: true

Size: 887 Bytes

Versions: 2

Compression:

Stored size: 887 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 self.import(datum, headers: nil, options: {})
    new(datum, headers: headers, options: options).import
  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 import
    processed_datum = [].push(@headers).compact
    CSV.foreach(@datum, @options) do |data|
      processed_datum.push(data)
    end
    return(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.2.0 lib/active_report/array.rb
active_report-1.1.0 lib/active_report/array.rb