Class: TSV::Dumper
- Inherits:
-
Object
- Object
- TSV::Dumper
- Defined in:
- lib/scout/tsv/dumper.rb
Instance Attribute Summary collapse
-
#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
Returns a new instance of Dumper.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# 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 @options = @options[:type] = @type @sout, @sin = Open.pipe Log.low{"Dumper pipe #{[Log.fingerprint(@sin), Log.fingerprint(@sout)] * " -> "}"} @initialized = false @filename = [:filename] @mutex = Mutex.new ConcurrentStream.setup(@sin, pair: @sout) ConcurrentStream.setup(@sout, pair: @sin) end |
Instance Attribute Details
#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 @options 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
126 127 128 |
# File 'lib/scout/tsv/dumper.rb', line 126 def abort(exception=nil) @sin.abort(exception) if @sin.respond_to?(:abort) end |
#add(key, value) ⇒ Object
86 87 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 |
# File 'lib/scout/tsv/dumper.rb', line 86 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 ? v * "|" : v } * @sep << "\n" else if Array === value if Array === value.first @sin << key + @sep + value.collect{|v| Array === v ? 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
72 73 74 75 |
# File 'lib/scout/tsv/dumper.rb', line 72 def all_fields return nil if fields.nil? [key_field] + fields end |
#close ⇒ Object
115 116 117 118 119 120 |
# File 'lib/scout/tsv/dumper.rb', line 115 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
138 139 140 |
# File 'lib/scout/tsv/dumper.rb', line 138 def digest_str fingerprint end |
#fields ⇒ Object
56 57 58 |
# File 'lib/scout/tsv/dumper.rb', line 56 def fields @options[:fields] end |
#fields=(fields) ⇒ Object
64 65 66 |
# File 'lib/scout/tsv/dumper.rb', line 64 def fields=(fields) @options[:fields] = fields end |
#fingerprint ⇒ Object
134 135 136 |
# File 'lib/scout/tsv/dumper.rb', line 134 def fingerprint "Dumper:{"<< Log.fingerprint(self.all_fields|| []) << "}" end |
#init(preamble: true) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/scout/tsv/dumper.rb', line 78 def init(preamble: true) header = Dumper.header(@options.merge(type: @type, sep: @sep, preamble: preamble)) @mutex.synchronize do @initialized = true @sin << header << "\n" if header and ! header.empty? end end |
#inspect ⇒ Object
142 143 144 |
# File 'lib/scout/tsv/dumper.rb', line 142 def inspect fingerprint end |
#key_field ⇒ Object
52 53 54 |
# File 'lib/scout/tsv/dumper.rb', line 52 def key_field @options[:key_field] end |
#key_field=(key_field) ⇒ Object
60 61 62 |
# File 'lib/scout/tsv/dumper.rb', line 60 def key_field=(key_field) @options[:key_field] = key_field end |
#set_stream(stream) ⇒ Object
46 47 48 49 50 |
# File 'lib/scout/tsv/dumper.rb', line 46 def set_stream(stream) @sin.close @sout.close @sout = @sin = stream end |
#stream ⇒ Object
122 123 124 |
# File 'lib/scout/tsv/dumper.rb', line 122 def stream @sout end |