Sha256: d3937f0268b86ee80f7ca3758f35c3e356bce75846ee5a1e224fa52842fdda76

Contents?: true

Size: 1.24 KB

Versions: 12

Compression:

Stored size: 1.24 KB

Contents

require "csv"

module Duracloud
  module TSV

    CHUNK_SIZE = 1024 * 16

    def csv
      @csv ||= CSV.new(tsv, csv_options)
    end

    def rows
      @rows ||= Enumerator.new do |e|
        table.each { |row| e << row.to_hash }
      end
    end

    def table
      csv.rewind
      csv.read
    ensure
      csv.rewind
    end

    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_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")
    end

    def to_s
      tsv.to_s
    end

    private

    def csv_options
      { col_sep: "\t",
        quote_char: "`",
        headers: true,
        return_headers: false,
        header_converters: header_converters,
      }
    end

    def header_converters
      lambda { |h| h.downcase.gsub(/-/, "_") }
    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
duracloud-client-0.10.2 lib/duracloud/tsv.rb
duracloud-client-0.10.1 lib/duracloud/tsv.rb
duracloud-client-0.10.0 lib/duracloud/tsv.rb
duracloud-client-0.9.1 lib/duracloud/tsv.rb
duracloud-client-0.9.0 lib/duracloud/tsv.rb
duracloud-client-0.8.0 lib/duracloud/tsv.rb
duracloud-client-0.7.2 lib/duracloud/tsv.rb
duracloud-client-0.7.1 lib/duracloud/tsv.rb
duracloud-client-0.7.0 lib/duracloud/tsv.rb
duracloud-client-0.6.0 lib/duracloud/tsv.rb
duracloud-client-0.5.0 lib/duracloud/tsv.rb
duracloud-client-0.4.0 lib/duracloud/tsv.rb