Class: Optimus::TabfileWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/tabfile_writer.rb

Overview

Writes an Optimus::Data object as a tab-delmited file – hopefully exactly like E-DataAid.

Instance Method Summary collapse

Constructor Details

#initialize(optimus_data, outstream, options = {}) ⇒ TabfileWriter

Create a writer, but don’t actually write the output. Valid things in the options hash: :write_top_line => true, if you want to include the filename

(if it's a file output stream) as the first line output


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tabfile_writer.rb', line 22

def initialize(optimus_data, outstream, options = {})
  standard_options = {
    :write_top_line => false,
    :columns => nil,
    :column_labels => true
  }
  good_opts = standard_options.merge(options)
  @optimus = optimus_data
  @outstream = outstream
  @write_top_line = good_opts[:write_top_line]
  @columns = good_opts[:columns] || @optimus.columns
  @column_labels = good_opts[:column_labels]
end

Instance Method Details

#writeObject

Write to the output stream.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tabfile_writer.rb', line 37

def write
  tsv = CSV.new(@outstream, {:col_sep => "\t"})
  if @write_top_line
    name = @outstream.respond_to?(:path) ? File.expand_path(@outstream.path.to_s) : ''
    tsv << [name]
  end
  if @column_labels
    tsv << @columns
  end
  @optimus.each do |row|
    vals = @columns.map { |col_name| row[col_name] }
    tsv << vals
  end
end