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.27 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.26 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.25 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.24 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.23 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.22 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.21 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.20 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.19 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.18 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.17 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.16 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.15 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.14 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.13 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.12 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.11 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.10 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.8 lib/jsonapi_compliable/stats/payload.rb
jsonapi_compliable-0.11.7 lib/jsonapi_compliable/stats/payload.rb