Class: ActiveModelArchive::Archiver::Csv
- Inherits:
-
Object
- Object
- ActiveModelArchive::Archiver::Csv
- Defined in:
- lib/active_model_archive/archiver/csv.rb
Direct Known Subclasses
Instance Method Summary collapse
Instance Method Details
#encode(object) ⇒ Object
13 14 15 16 17 |
# File 'lib/active_model_archive/archiver/csv.rb', line 13 def encode(object) object.as_archive.values_at(*object.archive_attributes).map do |value| value.respond_to?(:any?) && value.any? ? ActiveSupport::JSON.encode(value) : value end.to_csv(col_sep: separator) end |
#header(object) ⇒ Object
9 10 11 |
# File 'lib/active_model_archive/archiver/csv.rb', line 9 def header(object) object.archive_attributes.to_csv(col_sep: separator) end |
#parse(io) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/active_model_archive/archiver/csv.rb', line 19 def parse(io) first_line = true header = [] io.each do |line| values = line.parse_csv(col_sep: separator) if first_line first_line = false header = values else values.map! do |value| begin ActiveSupport::JSON.decode(value) rescue value end end yield Hash[header.zip(values)] end end end |