Class: ActiveForce::Bulk::Records

Inherits:
Object
  • Object
show all
Defined in:
lib/active_force/bulk/records.rb

Constant Summary collapse

NULL_VALUE =
'#N/A'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers:, data:) ⇒ Records

Returns a new instance of Records.



9
10
11
12
# File 'lib/active_force/bulk/records.rb', line 9

def initialize(headers:, data:)
  @headers = headers
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/active_force/bulk/records.rb', line 8

def data
  @data
end

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/active_force/bulk/records.rb', line 8

def headers
  @headers
end

Class Method Details

.parse_from_attributes(records) ⇒ Object



20
21
22
23
24
25
26
27
# 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) ⇒ Object

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



30
31
32
33
34
35
36
37
38
39
# 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

Instance Method Details

#to_csvObject



14
15
16
17
18
# 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