Class: Masticate::MaxRows

Inherits:
Base
  • Object
show all
Defined in:
lib/masticate/max_rows.rb

Instance Attribute Summary

Attributes inherited from Base

#csv_options, #filename, #input, #input_count, #output, #output_count

Instance Method Summary collapse

Methods inherited from Base

#emit, #execute, #get, #initialize, #standard_options, #with_input

Constructor Details

This class inherits a constructor from Masticate::Base

Instance Method Details

#configure(opts) ⇒ Object



5
6
7
8
9
10
# File 'lib/masticate/max_rows.rb', line 5

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

#crunch(row) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/masticate/max_rows.rb', line 16

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

#maxrows(opts) ⇒ Object



12
13
14
# File 'lib/masticate/max_rows.rb', line 12

def maxrows(opts)
  execute(opts)
end