Sha256: a1e8abbe9cfdfb5409ccc05154c4cc6b48adfeb09471a6a8d476f37cc61d72d5

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module Roar
  module JSON
    module JSONAPI
      # Instance method API for JSON API Documents representing a single Resource
      #
      # @api private
      module SingleResource
        # @see Document#to_hash
        def to_hash(options = {})
          document  = super(Options::Include.(options, mappings))
          unwrapped = options[:wrap] == false
          resource  = unwrapped ? document : document['data']
          resource['type'] = JSONAPI::MemberName.(self.class.type)

          links = Renderer::Links.new.(resource, options)
          meta  = render_meta(options)

          resource.reject! do |_, v| v && v.empty? end

          unless unwrapped
            included = resource.delete('included')

            HashUtils.store_if_any(document, 'included',
                                   Fragment::Included.(included, options))
          end

          HashUtils.store_if_any(resource, 'links', links)
          HashUtils.store_if_any(document, 'meta',  meta)

          document
        end

        private

        def mappings
          @mappings ||= begin
            mappings = {}
            mappings[:id]             = find_id_mapping
            mappings[:relationships]  = find_relationship_mappings
            mappings[:relationships]['_self'] = self.class.type
            mappings
          end
        end

        def find_id_mapping
          self.class.definitions.detect { |definition|
            definition[:as] && definition[:as].(:value) == 'id'
          }.name
        end

        def find_relationship_mappings
          included_definitions = self.class.definitions['included'].representer_module.definitions
          included_definitions.each_with_object({}) do |definition, hash|
            hash[definition.name] = definition.representer_module.type
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roar-jsonapi-0.0.3 lib/roar/json/json_api/single_resource.rb