Class: CSV::Writer
- Inherits:
-
Object
- Object
- CSV::Writer
- Defined in:
- lib/csv/writer.rb
Overview
Note: Don’t use this class directly. This is an internal class.
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#lineno ⇒ Object
readonly
A CSV::Writer receives an output, prepares the header, format and output.
Instance Method Summary collapse
-
#<<(row) ⇒ Object
Adds a new row.
-
#initialize(output, options) ⇒ Writer
constructor
A new instance of Writer.
-
#rewind ⇒ Object
Winds back to the beginning.
Constructor Details
#initialize(output, options) ⇒ Writer
Returns a new instance of Writer.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/csv/writer.rb', line 16 def initialize(output, ) @output = output @options = @lineno = 0 @fields_converter = nil prepare if @options[:write_headers] and @headers self << @headers end @fields_converter = @options[:fields_converter] end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
14 15 16 |
# File 'lib/csv/writer.rb', line 14 def headers @headers end |
#lineno ⇒ Object (readonly)
A CSV::Writer receives an output, prepares the header, format and output. It allows us to write new rows in the object and rewind it.
13 14 15 |
# File 'lib/csv/writer.rb', line 13 def lineno @lineno end |
Instance Method Details
#<<(row) ⇒ Object
Adds a new row
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/csv/writer.rb', line 31 def <<(row) case row when Row row = row.fields when Hash row = @headers.collect {|header| row[header]} end @headers ||= row if @use_headers @lineno += 1 if @fields_converter quoted_fields = [false] * row.size row = @fields_converter.convert(row, nil, lineno, quoted_fields) end i = -1 converted_row = row.collect do |field| i += 1 quote(field, i) end line = converted_row.join(@column_separator) + @row_separator if @output_encoding line = line.encode(@output_encoding) end @output << line self end |
#rewind ⇒ Object
Winds back to the beginning
64 65 66 67 |
# File 'lib/csv/writer.rb', line 64 def rewind @lineno = 0 @headers = nil if @options[:headers].nil? end |