Class: Mnemonic::Sink::CSV

Inherits:
Object
  • Object
show all
Defined in:
lib/mnemonic/sink/csv.rb

Instance Method Summary collapse

Constructor Details

#initialize(mnemonic, to = STDOUT, options = {}) ⇒ CSV

Returns a new instance of CSV.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mnemonic/sink/csv.rb', line 6

def initialize(mnemonic, to = STDOUT, options = {})
  @mnemonic = mnemonic

  col_count = mnemonic.metrics.length
  headers = mnemonic.metric_names

  @extra_column = options.delete(:extra)
  if @extra_column
    col_count += 1
    headers = headers.dup << 'Extra'.freeze
  end

  options[:headers] = headers
  options[:write_headers] = true
  @io = if to.kind_of? String
          @need_close = true
          File.open(to, 'w')
        else
          to
        end
  @csv = ::CSV.new(@io, options)
  @row = Array.new(col_count)
end

Instance Method Details

#closeObject



38
39
40
# File 'lib/mnemonic/sink/csv.rb', line 38

def close
  @io.close if @need_close
end

#drop!(extra) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mnemonic/sink/csv.rb', line 30

def drop!(extra)
  @mnemonic.metrics.each_with_index do |metric, i|
    @row[i] = metric.value
  end
  @row[-1] = extra if @extra_column
  @csv << @row
end