Class: Dap::Output::OutputCSV

Inherits:
Object
  • Object
show all
Includes:
FileDestination
Defined in:
lib/dap/output.rb

Overview

CSV Output

Constant Summary collapse

FIELD_WILDCARD =
'_'

Instance Attribute Summary collapse

Attributes included from FileDestination

#fd

Instance Method Summary collapse

Methods included from FileDestination

#close, #open, #sanitize, #start, #stop

Constructor Details

#initialize(args) ⇒ OutputCSV

Returns a new instance of OutputCSV.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/dap/output.rb', line 160

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.to_csv
  end

end

Instance Attribute Details

#delimiterObject

Returns the value of attribute delimiter.



155
156
157
# File 'lib/dap/output.rb', line 155

def delimiter
  @delimiter
end

#fieldsObject

Returns the value of attribute fields.



155
156
157
# File 'lib/dap/output.rb', line 155

def fields
  @fields
end

Instance Method Details

#write_record(doc) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/dap/output.rb', line 196

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.to_csv
end