Class: TSV::Dumper
- Inherits:
-
Object
- Object
- TSV::Dumper
- Defined in:
- lib/scout/tsv/dumper.rb
Instance Attribute Summary collapse
-
#compact ⇒ Object
Returns the value of attribute compact.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#initialized ⇒ Object
Returns the value of attribute initialized.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#options ⇒ Object
Returns the value of attribute options.
-
#sep ⇒ Object
Returns the value of attribute sep.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #abort(exception = nil) ⇒ Object
- #add(key, value) ⇒ Object
- #all_fields ⇒ Object
- #close ⇒ Object
- #digest_str ⇒ Object
- #fields ⇒ Object
- #fields=(fields) ⇒ Object
- #fingerprint ⇒ Object
- #init(preamble: true) ⇒ Object
-
#initialize(options = {}) ⇒ Dumper
constructor
A new instance of Dumper.
- #inspect ⇒ Object
- #key_field ⇒ Object
- #key_field=(key_field) ⇒ Object
- #set_stream(stream) ⇒ Object
- #stream ⇒ Object
- #tsv(*args) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Dumper
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/scout/tsv/dumper.rb', line 30 def initialize( = {}) = ..merge(sep: nil) if TSV::Parser === || TSV === @sep, @type = IndiferentHash. , :sep, :type, :sep => "\t", :type => :double @compact = [:compact] = [:type] = @type @sout, @sin = Open.pipe Log.low{"Dumper pipe #{[Log.fingerprint(@sin), Log.fingerprint(@sout)] * " -> "}"} @initialized = false @filename = [:filename] @mutex = Mutex.new @namespace = [:namespace] ConcurrentStream.setup(@sin, pair: @sout) ConcurrentStream.setup(@sout, pair: @sin, filename: filename) end |
Instance Attribute Details
#compact ⇒ Object
Returns the value of attribute compact.
29 30 31 |
# File 'lib/scout/tsv/dumper.rb', line 29 def compact @compact end |
#filename ⇒ Object
Returns the value of attribute filename.
29 30 31 |
# File 'lib/scout/tsv/dumper.rb', line 29 def filename @filename end |
#initialized ⇒ Object
Returns the value of attribute initialized.
29 30 31 |
# File 'lib/scout/tsv/dumper.rb', line 29 def initialized @initialized end |
#namespace ⇒ Object
Returns the value of attribute namespace.
29 30 31 |
# File 'lib/scout/tsv/dumper.rb', line 29 def namespace @namespace end |
#options ⇒ Object
Returns the value of attribute options.
29 30 31 |
# File 'lib/scout/tsv/dumper.rb', line 29 def end |
#sep ⇒ Object
Returns the value of attribute sep.
29 30 31 |
# File 'lib/scout/tsv/dumper.rb', line 29 def sep @sep end |
#type ⇒ Object
Returns the value of attribute type.
29 30 31 |
# File 'lib/scout/tsv/dumper.rb', line 29 def type @type end |
Class Method Details
.header(options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/scout/tsv/dumper.rb', line 3 def self.header(={}) key_field, fields, sep, header_hash, preamble, unnamed = IndiferentHash. , :key_field, :fields, :sep, :header_hash, :preamble, :unnamed, :sep => "\t", :header_hash => "#", :preamble => true if fields.nil? fields_str = nil elsif fields.empty? fields_str = "#{header_hash}#{key_field || "Id"}" else fields_str = "#{header_hash}#{key_field || "Id"}#{sep}#{fields*sep}" end if String === preamble preamble_str = preamble elsif preamble && .values.compact.any? preamble_str = "#: " + IndiferentHash.hash2string(.merge(serializer: nil)) else preamble_str = nil end preamble_str = preamble_str.strip if preamble_str [preamble_str, fields_str].compact * "\n" end |
Instance Method Details
#abort(exception = nil) ⇒ Object
128 129 130 |
# File 'lib/scout/tsv/dumper.rb', line 128 def abort(exception=nil) @sin.abort(exception) if @sin.respond_to?(:abort) end |
#add(key, value) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/scout/tsv/dumper.rb', line 88 def add(key, value) @mutex.synchronize do key = key.to_s unless String === key if value.nil? || (Array === value && value.empty?) @sin << key << "\n" else case @type when :single @sin << key + @sep + value.to_s << "\n" when :list, :flat @sin << key + @sep + value * @sep << "\n" when :double @sin << key + @sep + value.collect{|v| Array === v ? (@compact ? v.compact : v) * "|" : v } * @sep << "\n" else if Array === value if Array === value.first @sin << key + @sep + value.collect{|v| Array === v ? (@compact ? v.compact : v) * "|" : v } * @sep << "\n" else @sin << key + @sep + value * @sep << "\n" end else @sin << key + @sep + value.to_s << "\n" end end end end end |
#all_fields ⇒ Object
74 75 76 77 |
# File 'lib/scout/tsv/dumper.rb', line 74 def all_fields return nil if fields.nil? [key_field] + fields end |
#close ⇒ Object
117 118 119 120 121 122 |
# File 'lib/scout/tsv/dumper.rb', line 117 def close if @sin != @sout @sin.close if @sin.respond_to?(:close) && ! @sin.closed? @sin.join if @sin.respond_to?(:join) && ! @sin.joined? end end |
#digest_str ⇒ Object
140 141 142 |
# File 'lib/scout/tsv/dumper.rb', line 140 def digest_str fingerprint end |
#fields ⇒ Object
58 59 60 |
# File 'lib/scout/tsv/dumper.rb', line 58 def fields [:fields] end |
#fields=(fields) ⇒ Object
66 67 68 |
# File 'lib/scout/tsv/dumper.rb', line 66 def fields=(fields) [:fields] = fields end |
#fingerprint ⇒ Object
136 137 138 |
# File 'lib/scout/tsv/dumper.rb', line 136 def fingerprint "Dumper:{" + Log.fingerprint(self.all_fields|| []) << "}" end |
#init(preamble: true) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/scout/tsv/dumper.rb', line 80 def init(preamble: true) header = Dumper.header(.merge(type: @type, sep: @sep, preamble: preamble)) @mutex.synchronize do @initialized = true @sin << header + "\n" if header and ! header.empty? end end |
#inspect ⇒ Object
144 145 146 |
# File 'lib/scout/tsv/dumper.rb', line 144 def inspect fingerprint end |
#key_field ⇒ Object
54 55 56 |
# File 'lib/scout/tsv/dumper.rb', line 54 def key_field [:key_field] end |
#key_field=(key_field) ⇒ Object
62 63 64 |
# File 'lib/scout/tsv/dumper.rb', line 62 def key_field=(key_field) [:key_field] = key_field end |
#set_stream(stream) ⇒ Object
48 49 50 51 52 |
# File 'lib/scout/tsv/dumper.rb', line 48 def set_stream(stream) @sin.close @sout.close @sout = @sin = stream end |
#stream ⇒ Object
124 125 126 |
# File 'lib/scout/tsv/dumper.rb', line 124 def stream @sout end |
#tsv(*args) ⇒ Object
132 133 134 |
# File 'lib/scout/tsv/dumper.rb', line 132 def tsv(*args) TSV.open(stream, *args) end |