Module: Roar::JSON::JSONAPI::Document

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

Instance Method Summary collapse

Instance Method Details

#from_hash(hash, options = {}) ⇒ Object


186
187
188
# File 'lib/roar/json/json_api.rb', line 186

def from_hash(hash, options={})
  super(from_document(hash))
end

#to_hash(options = {}) ⇒ Object


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/roar/json/json_api.rb', line 162

def to_hash(options={})
  res = super(Options::Include.(options, self))

  links = Renderer::Links.new.call(res, options)
  # meta  = render_meta(options)

  relationships = render_relationships(res)
  included      = render_included(res)

  document = {
    data: data = {
      type: self.class.type,
      id: res.delete("id").to_s
    }
  }
  data[:attributes]    = res unless res.empty?
  data[:relationships] = relationships if relationships and relationships.any?

  Fragment::Links.(data, links, options)
  Fragment::Included.(document, included, options)

  document
end