Class: Shale::Adapter::CSV

Inherits:
Object
  • Object
show all
Defined in:
lib/shale/adapter/csv.rb

Overview

CSV adapter

Class Method Summary collapse

Class Method Details

.dump(obj, headers:, **options) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Serialize Array<Hash<String, any>> into CSV

Parameters:

  • obj (Array<Hash<String, any>>)

    Array<Hash<String, any>> object

  • headers (Array<String>)
  • options (Hash)

Returns:

  • (String)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shale/adapter/csv.rb', line 33

def self.dump(obj, headers:, **options)
  ::CSV.generate(**options) do |csv|
    obj.each do |row|
      values = []

      headers.each do |header|
        values << row[header] if row.key?(header)
      end

      csv << values
    end
  end
end

.load(csv, headers:, **options) ⇒ Array<Hash<String, any>>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse CSV into Array<Hash<String, any>>

Parameters:

  • csv (String)

    CSV document

  • headers (Array<String>)
  • options (Hash)

Returns:

  • (Array<Hash<String, any>>)


20
21
22
# File 'lib/shale/adapter/csv.rb', line 20

def self.load(csv, headers:, **options)
  ::CSV.parse(csv, headers: headers, **options).map(&:to_h)
end