Sha256: 4fe30517cb7ac20f3cf782e416b2471e2a174da2d0f02d1c478711036d7824ff

Contents?: true

Size: 966 Bytes

Versions: 14

Compression:

Stored size: 966 Bytes

Contents

# extract subset of columns from CSV
require "csv"

class Masticate::MaxRows < Masticate::Base
  def configure(opts)
    standard_options(opts)

    @groupby = opts[:by] or raise "missing field to group by"
    @maxon = opts[:max] or raise "missing field to max on"
  end

  def maxrows(opts)
    execute(opts)
  end

  def crunch(row)
    if !@headers
      @headers = row
      @index_by = row.index(@groupby) or raise "Unable to find column '#{@groupby}'"
      @index_max = row.index(@maxon) or raise "Unable to find column '#{@maxon}'"
      @accum = {}
      row
    elsif row.nil?
      # output the accumulated results
      @accum.each do |k,row|
        yield row
      end
    else
      key = row[@index_by]
      if !@accum[key]
        @accum[key] = row
      else
        oldscore = @accum[key][@index_max]
        newscore = row[@index_max]
        if newscore > oldscore
          @accum[key] = row
        end
      end
      nil
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
masticate-0.6.2 lib/masticate/max_rows.rb
masticate-0.6.1 lib/masticate/max_rows.rb
masticate-0.6.0 lib/masticate/max_rows.rb
masticate-0.5.1 lib/masticate/max_rows.rb
masticate-0.5.0 lib/masticate/max_rows.rb
masticate-0.4.2 lib/masticate/max_rows.rb
masticate-0.4.1 lib/masticate/max_rows.rb
masticate-0.4.0 lib/masticate/max_rows.rb
masticate-0.3.2 lib/masticate/max_rows.rb
masticate-0.3.1 lib/masticate/max_rows.rb
masticate-0.3 lib/masticate/max_rows.rb
masticate-0.2.3 lib/masticate/max_rows.rb
masticate-0.2.2 lib/masticate/max_rows.rb
masticate-0.2.1 lib/masticate/max_rows.rb