Class: Dap::Output::OutputLines
- Inherits:
-
Object
- Object
- Dap::Output::OutputLines
- Includes:
- FileDestination
- Defined in:
- lib/dap/output.rb
Overview
Line Output (CSV, TSV, etc) XXX: Quoted field handling is not supported, CSV should be a new output type
Constant Summary collapse
- FIELD_WILDCARD =
'_'
Instance Attribute Summary collapse
-
#delimiter ⇒ Object
Returns the value of attribute delimiter.
-
#fields ⇒ Object
Returns the value of attribute fields.
Attributes included from FileDestination
Instance Method Summary collapse
-
#initialize(args) ⇒ OutputLines
constructor
A new instance of OutputLines.
- #write_record(doc) ⇒ Object
Methods included from FileDestination
#close, #open, #sanitize, #start, #stop
Constructor Details
#initialize(args) ⇒ OutputLines
Returns a new instance of OutputLines.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dap/output.rb', line 73 def initialize(args) file = nil self.delimiter = "," self.fields = FIELD_WILDCARD header = false args.each do |str| k,v = str.split('=', 2) case k when 'file' file = v when 'header' header = ( v =~ /^[ty1]/i ? true : false ) when 'fields' self.fields = v.split(',') when 'delimiter' self.delimiter = case v.to_s when 'tab' "\t" when 'null' "\x00" else v end end end self.open(file) if header and not fields.include?(FIELD_WILDCARD) self.fd.puts self.fields.join(self.delimiter) self.fd.flush end end |
Instance Attribute Details
#delimiter ⇒ Object
Returns the value of attribute delimiter.
68 69 70 |
# File 'lib/dap/output.rb', line 68 def delimiter @delimiter end |
#fields ⇒ Object
Returns the value of attribute fields.
68 69 70 |
# File 'lib/dap/output.rb', line 68 def fields @fields end |
Instance Method Details
#write_record(doc) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/dap/output.rb', line 110 def write_record(doc) out = [] if self.fields.include?(FIELD_WILDCARD) doc.each_pair do |k,v| out << sanitize(v.to_s) end else self.fields.each do |k| out << sanitize(doc[k].to_s) end end return unless out.length > 0 self.fd.puts out.join(self.delimiter) self.fd.flush end |