lib/duracloud/tsv.rb in duracloud-client-0.3.0 vs lib/duracloud/tsv.rb in duracloud-client-0.4.0

- old
+ new

@@ -1,10 +1,12 @@ require "csv" module Duracloud module TSV + CHUNK_SIZE = 1024 * 16 + def csv @csv ||= CSV.new(tsv, csv_options) end def rows @@ -18,20 +20,40 @@ csv.read ensure csv.rewind end - def tsv - @tsv + def tsv(&block) + return unless tsv_source? + begin + tsv_source.rewind + if block_given? + while chunk = tsv_source.read(CHUNK_SIZE) + yield chunk + end + else + tsv_source.read + end + ensure + tsv_source.rewind + end end def load_tsv(io_or_str) - @tsv = io_or_str + @tsv_source = io_or_str.is_a?(String) ? StringIO.new(io_or_str, "rb") : io_or_str end + def tsv_source + @tsv_source + end + + def tsv_source? + !!@tsv_source + end + def load_tsv_file(path) - load_tsv(File.new(path, "rb")) + load_tsv File.new(path, "rb") end def to_s tsv.to_s end @@ -40,9 +62,10 @@ def csv_options { col_sep: "\t", quote_char: "`", headers: true, + return_headers: false, header_converters: header_converters, } end def header_converters