Module: Gorillib::Model::LoadFromTsv::ClassMethods

Defined in:
lib/gorillib/model/serialization/tsv.rb

Instance Method Summary collapse

Instance Method Details

#_each_from_tsv(filename, options = {}) { ... } ⇒ Object

Iterate a block over each line of a TSV file

Yields:

  • an object instantiated from each line in the file.

Raises:



23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object

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.

Returns:

  • with a block, returns nil; with no block, an array of this class' instances



42
43
44
45
46
47
48
49
50
# 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