Sha256: fa1c225afdb3237727824a5bbe8f5385ca1bd1900e02cea5c88cb8e895e1bc1a

Contents?: true

Size: 1.21 KB

Versions: 15

Compression:

Stored size: 1.21 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)
        @resource   = resource
        @query      = query
        @scope      = scope
      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|
              stats[name][calc] = function.call(@scope, name)
            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

15 entries across 15 versions & 2 rubygems

Version Path
graphiti-1.0.alpha.17 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.16 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.15 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.14 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.12 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.11 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.10 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.9 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.8 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.7 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.6 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.5 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.4 lib/graphiti/stats/payload.rb
graphiti-1.0.alpha.1 lib/graphiti/stats/payload.rb
graphiti-rb-1.0.alpha.1 lib/graphiti/stats/payload.rb