Class: PiecePipe::GroupByStep
- Defined in:
- lib/piece_pipe/group_by_step.rb
Overview
does a map / aggregate for you
Instance Attribute Summary
Attributes inherited from Step
Instance Method Summary collapse
- #generate_sequence ⇒ Object
-
#initialize(*keys) ⇒ GroupByStep
constructor
A new instance of GroupByStep.
- #process(item) ⇒ Object
Methods inherited from Step
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_sequence ⇒ Object
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 |