Module: Roar::JSON::JSONAPI::SingleResource Private

Defined in:
lib/roar/json/json_api/single_resource.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance method API for JSON API Documents representing a single Resource

Instance Method Summary collapse

Instance Method Details

#to_hash(options = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/roar/json/json_api/single_resource.rb', line 9

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