Class: Streamer::Functors::Group
- Inherits:
-
Functor
- Object
- Functor
- Streamer::Functors::Group
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
Instance Method Details
#accumulate(list, group_key, operand_key, operator) ⇒ Object
17
18
19
20
21
22
23
24
25
|
# File 'lib/streamer/functors/group.rb', line 17
def accumulate(list, group_key, operand_key, operator)
payload[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
|
#call ⇒ Object
5
6
7
|
# File 'lib/streamer/functors/group.rb', line 5
def call
group
end
|
#group ⇒ Object
9
10
11
12
13
14
15
|
# File 'lib/streamer/functors/group.rb', line 9
def group
list = options.fetch(:list)
group_key = options.fetch(:by)
operand_key = options.fetch(:operand)
operator = options.fetch(:operator).to_sym
accumulate(list, group_key, operand_key, operator)
end
|