Sha256: 3d52f5f491775193908c972f70be65ca87d399ceb05dcf000eb3f4f02fbef112

Contents?: true

Size: 961 Bytes

Versions: 3

Compression:

Stored size: 961 Bytes

Contents

# frozen_string_literal: true

class ActiveReport::Array < ActiveReport::Base

  attr_accessor :data, :headers, :options

  def initialize(data, headers: nil, options: {})
    @data = data
    @headers = headers
    @options = csv_options.merge(options)
  end

  def self.export(data, headers: nil, options: {})
    klass = new(data, headers: headers, options: options)
    klass.export
  end

  def self.import(data, headers: nil, options: {})
    klass = new(data, headers: headers, options: options)
    klass.import
  end

  def export
    @data = munge_first(@data)

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

  def import
    array = merge(@headers)

    CSV.foreach(@data, @options) do |row|
      row = encode_to_utf8(row) if csv_force_encoding?
      array.push(row)
    end

    array = array.flatten if array.size < 2
    metatransform(array)
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_report-5.1.0 lib/active_report/array.rb
active_report-5.0.1 lib/active_report/array.rb
active_report-5.0.0 lib/active_report/array.rb