class ActiveForce::Bulk::Records

Constants

NULL_VALUE

Attributes

data[R]
headers[R]

Public Class Methods

new(headers:, data:) click to toggle source
# File lib/active_force/bulk/records.rb, line 9
def initialize(headers:, data:)
  @headers = headers
  @data = data
end
parse_from_attributes(records) click to toggle source
# File lib/active_force/bulk/records.rb, line 20
def self.parse_from_attributes(records)
  # Sorting ensures that the headers line up with the values for the CSV
  headers = records.first.keys.sort.map(&:to_s)
  data = records.map do |r|
     r.transform_values { |v| transform_value_for_sf(v) }.sort.pluck(-1)
  end
  new(headers: headers, data: data)
end
transform_value_for_sf(value) click to toggle source

SF expects a special value for setting a column to be NULL.

# File lib/active_force/bulk/records.rb, line 30
def self.transform_value_for_sf(value)
  case value
  when NilClass
    NULL_VALUE
  when Time
    value.iso8601
  else
    value.to_s
  end
end

Public Instance Methods

to_csv() click to toggle source
# File lib/active_force/bulk/records.rb, line 14
def to_csv
  CSV.generate(String.new, headers: headers, write_headers: true) do |csv|
    data.each { |row| csv << row }
  end
end