module Gorillib::Model::LoadFromTsv::ClassMethods
Public Instance Methods
_each_from_tsv(filename, options={}) { |from_tuple(*tuple)| ... }
click to toggle source
Iterate a block over each line of a TSV file
@raise [Gorillib::Model::RawDataMismatchError] if a line has too many or too few fields @yield an object instantiated from each line in the file.
# File lib/gorillib/model/serialization/tsv.rb, line 23 def _each_from_tsv(filename, options={}) options = tsv_options.merge(options) num_fields = options.delete(:num_fields){ (fields.length .. fields.length) } max_fields = num_fields.max # need to make sure "1\t2\t\t\t" becomes ["1","2","","",""] # _each_raw_line(filename, options) do |line| tuple = line.split("\t", max_fields) unless num_fields.include?(tuple.length) then raise Gorillib::Model::RawDataMismatchError, "yark, spurious fields: #{tuple.inspect}" ; end yield from_tuple(*tuple) end end
load_tsv(*args)
click to toggle source
With a block, calls block on each object in turn (and returns nil)
With no block, accumulates all the instances into the array it returns. As opposed to the with-a-block case, the memory footprint of this increases as the filesize does, so use caution with large files.
@return with a block, returns nil; with no block, an array of this class’ instances
# File lib/gorillib/model/serialization/tsv.rb, line 42 def load_tsv(*args) if block_given? _each_from_tsv(*args, &Proc.new) else objs = [] _each_from_tsv(*args){|obj| objs << obj } objs end end