Sha256: a1d2ae01cff389a81dd8c2396ecffc932b856a8b24359a7ef36c43bc2c00654a
Contents?: true
Size: 1.36 KB
Versions: 9
Compression:
Stored size: 1.36 KB
Contents
module Graphiti module Stats # 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 } } } # } class Payload def initialize(resource, query, scope, data) @resource = resource @query = query @scope = scope @data = data end # Generate the payload for +{ meta: { stats: { ... } } }+ # Loops over all calculations, computes then, and gives back # a hash of stats and their results. # @return [Hash] the generated payload def generate {}.tap do |stats| @query.stats.each_pair do |name, calculation| stats[name] = {} each_calculation(name, calculation) do |calc, function| args = [@scope, name] args << @resource.context if function.arity >= 3 args << @data if function.arity == 4 stats[name][calc] = function.call(*args) end end end end private def each_calculation(name, calculations) calculations.each do |calc| function = @resource.stat(name, calc) yield calc, function end end end end end
Version data entries
9 entries across 9 versions & 1 rubygems