Sha256: 260cdbbf575657c69f2d301409814ae496a97f112d77e09a53451acd63b16cd6

Contents?: true

Size: 1.42 KB

Versions: 57

Compression:

Stored size: 1.42 KB

Contents

module JsonapiCompliable
  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
      # @param [Resource] resource the resource instance
      # @param [Hash] query_hash the Query#to_hash for the current resource
      # @param scope the scope we are chaining/modifying
      def initialize(resource, query_hash, scope)
        @resource   = resource
        @query_hash = query_hash[:stats]
        @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_hash.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

57 entries across 57 versions & 1 rubygems

Version Path
jsonapi_compliable-0.11.6 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.5 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.4 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.3 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.2 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.1 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.0 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.10.9 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.10.7 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.10.6 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.10.5 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.10.4 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.10.3 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.10.2 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.10.1 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.10.0 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.9.2 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.9.1 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.9.0 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.8.0 lib/jsonapi_compliable/stats/payload.rb