Class: TSV::Dumper

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/tsv/dumper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, filename = nil) ⇒ Dumper

Returns a new instance of Dumper.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rbbt/tsv/dumper.rb', line 13

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

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



3
4
5
# File 'lib/rbbt/tsv/dumper.rb', line 3

def filename
  @filename
end

#in_streamObject

Returns the value of attribute in_stream.



3
4
5
# File 'lib/rbbt/tsv/dumper.rb', line 3

def in_stream
  @in_stream
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/rbbt/tsv/dumper.rb', line 3

def options
  @options
end

#sepObject

Returns the value of attribute sep.



3
4
5
# File 'lib/rbbt/tsv/dumper.rb', line 3

def sep
  @sep
end

#streamObject

Returns the value of attribute stream.



3
4
5
# File 'lib/rbbt/tsv/dumper.rb', line 3

def stream
  @stream
end

Class Method Details

.stream(options = {}, filename = nil, &block) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/rbbt/tsv/dumper.rb', line 4

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

.values_to_s(values, fields = nil, sep = "\t") ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rbbt/tsv/dumper.rb', line 25

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
    if fields.nil? 
      sep + (values.collect{|v| Array === v ? v * "|" : v} * sep) << "\n"
    elsif fields.empty?
      "\n"
    else
      sep + (values.collect{|v| Array === v ? v * "|" : v} * sep) << "\n"
    end
  else
    if fields.nil?
      sep + values.to_s + "\n"
    elsif fields.empty?
      "\n"
    else
      sep + values.to_s << "\n"
    end
  end
end

Instance Method Details

#add(k, v) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rbbt/tsv/dumper.rb', line 64

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

#closeObject



85
86
87
# File 'lib/rbbt/tsv/dumper.rb', line 85

def close
  close_in
end

#close_inObject



80
81
82
83
# File 'lib/rbbt/tsv/dumper.rb', line 80

def close_in
  @in_stream.join if @in_stream.respond_to?(:join) && ! @in_stream.joined?
  @in_stream.close unless @in_stream.closed?
end

#close_outObject



76
77
78
# File 'lib/rbbt/tsv/dumper.rb', line 76

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

#init(init_options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/rbbt/tsv/dumper.rb', line 53

def init(init_options = {})
  options = @options.dup
  key_field, fields = Misc.process_options options, :key_field, :fields

  str = TSV.header_lines(key_field, fields, options.merge(init_options || {}))

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

  @in_stream.puts str
end