Sha256: 6d38de368dccd9d2fed6d43204a824574c8fc825ab1a6b826dd7942ddc8e177d

Contents?: true

Size: 542 Bytes

Versions: 1

Compression:

Stored size: 542 Bytes

Contents

# frozen_string_literal: true

module JSONAPIHelpers
  module Serializers
    class Datum
      def initialize(jsonapi_datums)
        @datums = jsonapi_datums
      end

      def to_h
        {
          data: @datums.map { |data| data.to_h(shallow: true) },
          meta: {
            total: @datums.length
          }
        }
      end

      # Rails is awkward and calls #to_json with a context argument
      # NOTE: Rails only method Hash#to_json
      def to_json(_context = nil)
        to_h.to_json
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jsonapi_helpers-0.2.0 lib/jsonapi_helpers/serializers/datum.rb