Class: TSV::Dumper
- Inherits:
-
Object
- Object
- TSV::Dumper
- Defined in:
- lib/rbbt/tsv/dumper.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#in_stream ⇒ Object
Returns the value of attribute in_stream.
-
#options ⇒ Object
Returns the value of attribute options.
-
#sep ⇒ Object
Returns the value of attribute sep.
-
#stream ⇒ Object
Returns the value of attribute stream.
Class Method Summary collapse
- .stream(options = {}, filename = nil, stream = nil, &block) ⇒ Object
- .values_to_s(values, fields = nil, sep = "\t") ⇒ Object
Instance Method Summary collapse
- #add(k, v) ⇒ Object
- #close ⇒ Object
- #close_in ⇒ Object
- #close_out ⇒ Object
- #init(init_options = {}) ⇒ Object
-
#initialize(options, filename = nil) ⇒ Dumper
constructor
A new instance of Dumper.
- #set_stream(stream) ⇒ Object
Constructor Details
#initialize(options, filename = nil) ⇒ Dumper
Returns a new instance of Dumper.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rbbt/tsv/dumper.rb', line 24 def initialize(, filename = nil) if TSV === @options = ..merge(:key_field => .key_field, :fields => .fields) @filename ||= .filename else @options = @filename = filename end @filename ||= Misc.fingerprint @stream, @in_stream = Misc.pipe end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
3 4 5 |
# File 'lib/rbbt/tsv/dumper.rb', line 3 def filename @filename end |
#in_stream ⇒ Object
Returns the value of attribute in_stream.
3 4 5 |
# File 'lib/rbbt/tsv/dumper.rb', line 3 def in_stream @in_stream end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/rbbt/tsv/dumper.rb', line 3 def @options end |
#sep ⇒ Object
Returns the value of attribute sep.
3 4 5 |
# File 'lib/rbbt/tsv/dumper.rb', line 3 def sep @sep end |
#stream ⇒ Object
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, stream = nil, &block) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/rbbt/tsv/dumper.rb', line 4 def self.stream( = {}, filename = nil, stream = nil, &block) dumper = TSV::Dumper.new , filename if stream dumper.set_stream stream if stream yield dumper stream else thread = Thread.new(Thread.current) do |parent| yield dumper end ConcurrentStream.setup(dumper.stream, threads: thread) end end |
.values_to_s(values, fields = nil, sep = "\t") ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rbbt/tsv/dumper.rb', line 36 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
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rbbt/tsv/dumper.rb', line 75 def add(k,v) @fields ||= @options[:fields] @sep ||= @options[:sep] begin Thread.pass while IO.select(nil, [@in_stream],nil,1).nil? if IO === @in_stream @in_stream << k << TSV::Dumper.values_to_s(v, @fields, @sep) rescue IOError rescue Exception raise $! end end |
#close ⇒ Object
96 97 98 |
# File 'lib/rbbt/tsv/dumper.rb', line 96 def close close_in unless @in_stream == @stream end |
#close_in ⇒ Object
91 92 93 94 |
# File 'lib/rbbt/tsv/dumper.rb', line 91 def close_in @in_stream.join if @in_stream.respond_to?(:join) && ! @in_stream.joined? @in_stream.close if @in_stream.respond_to?(:close) && ! @in_stream.closed? end |
#close_out ⇒ Object
87 88 89 |
# File 'lib/rbbt/tsv/dumper.rb', line 87 def close_out @stream.close unless StringIO === @stream || @stream.closed? end |
#init(init_options = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rbbt/tsv/dumper.rb', line 64 def init( = {}) = @options.dup key_field, fields = Misc. , :key_field, :fields str = TSV.header_lines(key_field, fields, .merge( || {})) Thread.pass while IO.select(nil, [@in_stream],nil,1).nil? if IO === @in_stream @in_stream << str end |
#set_stream(stream) ⇒ Object
18 19 20 21 22 |
# File 'lib/rbbt/tsv/dumper.rb', line 18 def set_stream(stream) @stream.close @in_stream.close @in_stream = @stream = stream end |