Sha256: 16ed906d7fffd98370406366e8dd2d1cc40be1b2b2e54fcd6c64075ec9c334a9

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

class ActiveReport::Array < ActiveReport::Base

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

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

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

  def export
    @datum = munge_first(@datum)

    if @stream == true
      Enumerator.new do |csv|
        csv << @headers unless @headers.nil?
        @datum.each { |row| csv << row }
      end
    else
      CSV.generate(@options) do |csv|
        csv << @headers unless @headers.nil?
        @datum.each { |row| csv << row }
      end
    end
  end

  def import
    array = merge(@headers)

    CSV.foreach(@datum, @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

1 entries across 1 versions & 1 rubygems

Version Path
active_report-5.2.0 lib/active_report/array.rb