Sha256: c329723eb63cb013fdec24cf0dbf47b6a204652d74ec433d457b9c4a27346890

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

# Part of the Optimus package for managing E-Prime data
# 
# Copyright (C) 2008 Board of Regents of the University of Wisconsin System
# 
# Written by Nathan Vack <njvack@wisc.edu>, at the Waisman Laborotory for Brain
# Imaging and Behavior, University of Wisconsin - Madison

# Just use the standard Ruby CSV processing library; it'll make our lives
# way easier (by handling in-band tabs, etc)
require 'csv'

module Eprime
  
  # Writes an Eprime::Data object as a tab-delmited file -- hopefully exactly
  # like E-DataAid.
  class 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
    def initialize(eprime_data, outstream, options = {})
      standard_options = {
        :write_top_line => false,
        :columns => nil,
        :column_labels => true
      }
      good_opts = standard_options.merge(options)
      @eprime = eprime_data
      @outstream = outstream
      @write_top_line = good_opts[:write_top_line]
      @columns = good_opts[:columns] || @eprime.columns
      @column_labels = good_opts[:column_labels]
    end
    
    # Write to the output stream.
    def write
      CSV::Writer.generate(@outstream, "\t") do |tsv|
        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
        @eprime.each do |row|
          vals = @columns.map { |col_name| row[col_name] }
          tsv << vals
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
optimus-ep-0.6.0 lib/tabfile_writer.rb
optimus-ep-0.6.5 lib/tabfile_writer.rb
optimus-ep-0.6.9 lib/tabfile_writer.rb
optimus-ep-0.6.91 lib/tabfile_writer.rb