Class: Burner::Library::Serialize::Csv

Inherits:
JobWithRegister show all
Defined in:
lib/burner/library/serialize/csv.rb

Overview

Take an array of arrays and create a CSV. You can optionally pre-pend a byte order mark, see Burner::Modeling::ByteOrderMark for acceptable options.

Expected Payload input: array of arrays. Payload output: a serialized CSV string.

Constant Summary

Constants inherited from JobWithRegister

JobWithRegister::BLANK

Instance Attribute Summary collapse

Attributes inherited from JobWithRegister

#register

Attributes inherited from Job

#name

Instance Method Summary collapse

Methods included from Util::Arrayable

#array

Constructor Details

#initialize(byte_order_mark: nil, name: '', register: DEFAULT_REGISTER) ⇒ Csv

Returns a new instance of Csv.



21
22
23
24
25
26
27
# File 'lib/burner/library/serialize/csv.rb', line 21

def initialize(byte_order_mark: nil, name: '', register: DEFAULT_REGISTER)
  super(name: name, register: register)

  @byte_order_mark = Modeling::ByteOrderMark.resolve(byte_order_mark)

  freeze
end

Instance Attribute Details

#byte_order_markObject (readonly)

Returns the value of attribute byte_order_mark.



19
20
21
# File 'lib/burner/library/serialize/csv.rb', line 19

def byte_order_mark
  @byte_order_mark
end

Instance Method Details

#perform(_output, payload) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/burner/library/serialize/csv.rb', line 29

def perform(_output, payload)
  serialized_rows = CSV.generate(options) do |csv|
    array(payload[register]).each do |row|
      csv << row
    end
  end

  payload[register] = "#{byte_order_mark}#{serialized_rows}"
end