Class: Streamer::Functors::Group

Inherits:
Functor
  • Object
show all
Defined in:
lib/streamer/functors/group.rb

Overview

Group groups a list by a property and a function

Instance Attribute Summary

Attributes inherited from Functor

#options, #payload

Instance Method Summary collapse

Methods inherited from Functor

#class_name, #initialize, #type_name

Constructor Details

This class inherits a constructor from Streamer::Functors::Functor

Instance Method Details

#accumulate(list, group_key, operand_key, operator) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/streamer/functors/group.rb', line 16

def accumulate(list, group_key, operand_key, operator)
  list.each_with_object({}) do |item, val|
    val[item[group_key]] =
      (val[item[group_key]] || 0.0).send(
        operator,
        (item[operand_key].to_f || 0)
      )
  end
end

#callObject



5
6
7
# File 'lib/streamer/functors/group.rb', line 5

def call
  group
end

#groupObject



9
10
11
12
13
14
# File 'lib/streamer/functors/group.rb', line 9

def group
  group_key = options.fetch(:by)
  operand_key = options.fetch(:operand)
  operator = options.fetch(:operator).to_sym
  accumulate(list, group_key, operand_key, operator)
end