Class: Optimus::TabfileWriter
- Inherits:
-
Object
- Object
- Optimus::TabfileWriter
- 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
-
#initialize(optimus_data, outstream, options = {}) ⇒ TabfileWriter
constructor
Create a writer, but don’t actually write the output.
-
#write ⇒ Object
Write to the output stream.
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, = {}) = { :write_top_line => false, :columns => nil, :column_labels => true } good_opts = .merge() @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
#write ⇒ Object
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.(@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 |