Class: Philiprehberger::CsvKit::Writer
- Inherits:
-
Object
- Object
- Philiprehberger::CsvKit::Writer
- Defined in:
- lib/philiprehberger/csv_kit/writer.rb
Overview
Generates CSV output from arrays of hashes or arrays.
Defined Under Namespace
Classes: StreamWriter
Class Method Summary collapse
-
.stream(io, headers:, dialect: nil) {|StreamWriter| ... } ⇒ IO
Stream CSV rows incrementally to an IO object without buffering.
Instance Method Summary collapse
-
#initialize(headers:) ⇒ Writer
constructor
A new instance of Writer.
-
#write(rows) ⇒ String
Write rows to a CSV string.
-
#write_to(rows, io) ⇒ IO
Write rows to an IO object.
Constructor Details
#initialize(headers:) ⇒ Writer
Returns a new instance of Writer.
8 9 10 |
# File 'lib/philiprehberger/csv_kit/writer.rb', line 8 def initialize(headers:) @headers = headers.map(&:to_sym) end |
Class Method Details
.stream(io, headers:, dialect: nil) {|StreamWriter| ... } ⇒ IO
Stream CSV rows incrementally to an IO object without buffering.
19 20 21 22 23 |
# File 'lib/philiprehberger/csv_kit/writer.rb', line 19 def self.stream(io, headers:, dialect: nil, &block) writer = StreamWriter.new(io, headers: headers, dialect: dialect) block.call(writer) io end |
Instance Method Details
#write(rows) ⇒ String
Write rows to a CSV string.
29 30 31 |
# File 'lib/philiprehberger/csv_kit/writer.rb', line 29 def write(rows) generate_csv(rows, StringIO.new).string end |
#write_to(rows, io) ⇒ IO
Write rows to an IO object.
38 39 40 41 |
# File 'lib/philiprehberger/csv_kit/writer.rb', line 38 def write_to(rows, io) generate_csv(rows, io) io end |