Sha256: b90d81e991e9bb1a676b3acce0acf253d6297b94cdb9e1aab7e6937789c4dee8

Contents?: true

Size: 1.94 KB

Versions: 9

Compression:

Stored size: 1.94 KB

Contents

require 'matrix'

module TSV
  def reorder(key_field = nil, fields = nil, merge: true, one2one: true, **kwargs) 
    res = self.annotate({})
    res.type = kwargs[:type] if kwargs.include?(:type)
    kwargs[:one2one] = one2one
    key_field_name, field_names = traverse key_field, fields, **kwargs do |k,v|
      if @type == :double && merge && res.include?(k)
        current = res[k]
        if merge == :concat
          v.each_with_index do |new,i|
            next if new.empty?
            current[i].concat(new)
          end
        else
          merged = []
          v.each_with_index do |new,i|
            next if new.empty?
            merged[i] = current[i] + new
          end
          res[k] = merged
        end
      else
        res[k] = v
      end
    end
    res.key_field = key_field_name
    res.fields = field_names
    res
  end

  def slice(fields, **kwargs)
    reorder :key, fields, **kwargs
  end

  def column(field, **kwargs)
    new_type = case type
               when :double, :flat
                 :flat
               else
                 :single
               end

    kwargs[:type] = new_type
    slice(field, **kwargs)
  end

  def transpose_list(key_field="Unkown ID")
    new_fields = keys.dup
    new = self.annotate({})
    TSV.setup(new, :key_field => key_field, :fields => new_fields, :type => type, :filename => filename, :identifiers => identifiers)

    m = Matrix.rows values 
    new_rows = m.transpose.to_a

    fields.zip(new_rows) do |key,row|
      new[key] = row
    end

    new
  end

  def transpose_double(key_field = "Unkown ID")
    sep = "-!SEP--#{rand 10000}!-"
    tmp = self.to_list{|v| v * sep}
    new = tmp.transpose_list(key_field)
    new.to_double{|v| v.split(sep)}
  end

  def transpose(key_field = "Unkown ID")
    case type
    when :single, :flat
      self.to_list.transpose_list key_field
    when :list
      transpose_list key_field
    when :double
      transpose_double key_field
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
scout-gear-10.4.0 lib/scout/tsv/util/reorder.rb
scout-gear-10.3.0 lib/scout/tsv/util/reorder.rb
scout-gear-10.2.0 lib/scout/tsv/util/reorder.rb
scout-gear-10.1.0 lib/scout/tsv/util/reorder.rb
scout-gear-10.0.1 lib/scout/tsv/util/reorder.rb
scout-gear-9.1.0 lib/scout/tsv/util/reorder.rb
scout-gear-9.0.0 lib/scout/tsv/util/reorder.rb
scout-gear-8.1.0 lib/scout/tsv/util/reorder.rb
scout-gear-8.0.0 lib/scout/tsv/util/reorder.rb