Class: Graphiti::Stats::Payload
Overview
Generate the stats payload so we can return it in the response.
{
data: [...],
meta: { stats: the_generated_payload }
}
For example:
{
data: [...],
meta: { stats: { total: { count: 100 } } }
}
Instance Method Summary collapse
- #calculate_stat(name, function) ⇒ Object
-
#generate ⇒ Hash
Generate the payload for
{ meta: { stats: { ... } } }Loops over all calculations, computes then, and gives back a hash of stats and their results. -
#initialize(resource, query, scope, data, group_by: nil) ⇒ Payload
constructor
A new instance of Payload.
Constructor Details
#initialize(resource, query, scope, data, group_by: nil) ⇒ Payload
Returns a new instance of Payload.
17 18 19 20 21 22 23 |
# File 'lib/graphiti/stats/payload.rb', line 17 def initialize(resource, query, scope, data, group_by: nil) @resource = resource @query = query @scope = scope @data = data @group_by = group_by end |
Instance Method Details
#calculate_stat(name, function) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/graphiti/stats/payload.rb', line 41 def calculate_stat(name, function) args = [@scope, @resource.model_attribute_for(name)] args << @resource.context if function.arity >= 3 args << @data if function.arity == 4 translate_group_keys(function.call(*args)) end |
#generate ⇒ Hash
Generate the payload for { meta: { stats: { ... } } }
Loops over all calculations, computes then, and gives back
a hash of stats and their results.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/graphiti/stats/payload.rb', line 29 def generate {}.tap do |stats| @query.stats.each_pair do |name, calculation| stats[name] = {} each_calculation(name, calculation) do |calc, function| stats[name][calc] = calculate_stat(name, function) end end end end |