Class: RdbCSV::CSV
- Inherits:
-
Object
- Object
- RdbCSV::CSV
- Includes:
- RdbCSVReader, RdbCSVRow
- Defined in:
- lib/rdb_csv.rb
Constant Summary collapse
- WRITE_BUFFER_SIZE =
1000
Instance Method Summary collapse
- #<<(array) ⇒ Object
- #buffer_write ⇒ Object
- #each ⇒ Object
-
#initialize(f, mode, options) ⇒ CSV
constructor
A new instance of CSV.
Constructor Details
#initialize(f, mode, options) ⇒ CSV
Returns a new instance of CSV.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rdb_csv.rb', line 10 def initialize(f, mode, ) @f = f @mode = [:mode] @db = [:db] @delimiter = [:delimiter] || "\t" @escape = "\\" @linefeed = "\n" quote = [:quote] || '"' # quote option is valid only mysql @quote = [:db] == :mysql ? quote : '"' end |
Instance Method Details
#<<(array) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rdb_csv.rb', line 35 def <<(array) raise IOError if @mode == 'r' @lines ||= [] row = Row.new(array) line = row.join(@escape, @db, @delimiter) @lines << line + @linefeed buffer_write if @lines.size >= WRITE_BUFFER_SIZE end |
#buffer_write ⇒ Object
46 47 48 49 50 51 |
# File 'lib/rdb_csv.rb', line 46 def buffer_write return if @lines.nil? || @lines.size.zero? @f.write(@lines.join) @lines = [] end |
#each ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/rdb_csv.rb', line 23 def each raise IOError if @mode == 'w' reader = Reader.new(@f, @db, @delimiter, escape: @escape, linefeed: @linefeed, quote: @quote) reader.each_line do |row| yield row.unescape(@escape, @db, @delimiter) end end |