Class: Shale::Adapter::CSV
- Inherits:
-
Object
- Object
- Shale::Adapter::CSV
- Defined in:
- lib/shale/adapter/csv.rb
Overview
CSV adapter
Class Method Summary collapse
-
.dump(obj, headers:, **options) ⇒ String
private
Serialize Array<Hash<String, any>> into CSV.
-
.load(csv, headers:, **options) ⇒ Array<Hash<String, any>>
private
Parse CSV into Array<Hash<String, any>>.
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
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:, **) ::CSV.generate(**) 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>>
20 21 22 |
# File 'lib/shale/adapter/csv.rb', line 20 def self.load(csv, headers:, **) ::CSV.parse(csv, headers: headers, **).map(&:to_h) end |