Class: Dap::Output::OutputLines

Inherits:
Object
  • Object
show all
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

Attributes included from FileDestination

#fd

Instance Method Summary collapse

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

#delimiterObject

Returns the value of attribute delimiter.



68
69
70
# File 'lib/dap/output.rb', line 68

def delimiter
  @delimiter
end

#fieldsObject

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