class Graphiti::Stats::Payload

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 } } }
}

Public Class Methods

new(resource, query, scope, data) click to toggle source
# File lib/graphiti/stats/payload.rb, line 17
def initialize(resource, query, scope, data)
  @resource = resource
  @query = query
  @scope = scope
  @data = data
end

Public Instance Methods

calculate_stat(name, function) click to toggle source
# File lib/graphiti/stats/payload.rb, line 40
def calculate_stat(name, function)
  args = [@scope, name]
  args << @resource.context if function.arity >= 3
  args << @data if function.arity == 4
  function.call(*args)
end
generate() click to toggle source

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

# File lib/graphiti/stats/payload.rb, line 28
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

Private Instance Methods

each_calculation(name, calculations) { |calc, function| ... } click to toggle source
# File lib/graphiti/stats/payload.rb, line 49
def each_calculation(name, calculations)
  calculations.each do |calc|
    function = @resource.stat(name, calc)
    yield calc, function
  end
end