Class: PiecePipe::GroupByStep

Inherits:
Step
  • Object
show all
Defined in:
lib/piece_pipe/group_by_step.rb

Overview

does a map / aggregate for you

Instance Attribute Summary

Attributes inherited from Step

#source

Instance Method Summary collapse

Methods inherited from Step

#to_enum

Constructor Details

#initialize(*keys) ⇒ GroupByStep

Returns a new instance of GroupByStep.



5
6
7
8
# File 'lib/piece_pipe/group_by_step.rb', line 5

def initialize(*keys)
  @hash = {}
  @keys = keys
end

Instance Method Details

#generate_sequenceObject



26
27
28
29
30
31
# File 'lib/piece_pipe/group_by_step.rb', line 26

def generate_sequence
  super
  @hash.each do |key,values|
    produce key => values
  end
end

#process(item) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/piece_pipe/group_by_step.rb', line 10

def process(item)
  key = nil
  if @keys.size > 1
    key = []
    @keys.each do |k|
      key << item[k]
    end
  else
    key = item[@keys.first]
  end

  val = item
  @hash[key] ||= []
  @hash[key] << item
end