Sha256: 5bf8e2ef6e703ad09f032af6595af7ede42fb532454efd476151d0acf7260b53

Contents?: true

Size: 1.85 KB

Versions: 13

Compression:

Stored size: 1.85 KB

Contents

module TSV
  class Dumper
    attr_accessor :in_stream, :stream, :options, :filename, :sep
    def self.stream(options = {}, filename = nil, &block)
      dumper = TSV::Dumper.new options, filename
      Thread.new(Thread.current) do |parent|
        yield dumper
        dumper.close
      end
      dumper.stream
    end

    def initialize(options, filename = nil)
      if TSV  === options
        @options = options.options.merge(:key_field => options.key_field, :fields => options.fields)
        @filename ||= options.filename
      else
        @options = options
        @filename = filename
      end
      @filename ||= Misc.fingerprint options
      @stream, @in_stream = Misc.pipe
    end

    def self.values_to_s(values, fields = nil, sep = "\t")
      sep = "\t" if sep.nil?
      case values
      when nil
        if fields.nil? or fields.empty?
          "\n"
        else
          sep + ([""] * fields.length) * sep << "\n"
        end
      when Array
        sep + values.collect{|v| Array === v ? v * "|" : v} * sep << "\n"
      else
        sep + values.to_s << "\n"
      end
    end

    def init
      options = @options.dup
      key_field, fields = Misc.process_options options, :key_field, :fields

      str = TSV.header_lines(key_field, fields, options)

      Thread.pass while IO.select(nil, [@in_stream],nil,1).nil?

      @in_stream.puts str
    end

    def add(k,v)
      @fields ||= @options[:fields]
      @sep ||= @options[:sep]
      begin
        Thread.pass while IO.select(nil, [@in_stream],nil,1).nil?
        @in_stream << k << TSV::Dumper.values_to_s(v, @fields, @sep)
      rescue IOError
      rescue Exception
        raise $!
      end
    end

    def close_out
      @stream.close unless @stream.closed?
    end

    def close_in
      @in_stream.close unless @in_stream.closed?
    end

    def close
      close_in
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rbbt-util-5.15.2 lib/rbbt/tsv/dumper.rb
rbbt-util-5.15.1 lib/rbbt/tsv/dumper.rb
rbbt-util-5.14.39 lib/rbbt/tsv/dumper.rb
rbbt-util-5.14.38 lib/rbbt/tsv/dumper.rb
rbbt-util-5.14.37 lib/rbbt/tsv/dumper.rb
rbbt-util-5.14.36 lib/rbbt/tsv/dumper.rb
rbbt-util-5.14.35 lib/rbbt/tsv/dumper.rb
rbbt-util-5.14.34 lib/rbbt/tsv/dumper.rb
rbbt-util-5.14.33 lib/rbbt/tsv/dumper.rb
rbbt-util-5.14.32 lib/rbbt/tsv/dumper.rb
rbbt-util-5.14.31 lib/rbbt/tsv/dumper.rb
rbbt-util-5.14.30 lib/rbbt/tsv/dumper.rb
rbbt-util-5.14.29 lib/rbbt/tsv/dumper.rb