Module: JSONAPI::Serializer
- Defined in:
- lib/jsonapi-serializers.rb,
lib/jsonapi-serializers/version.rb,
lib/jsonapi-serializers/serializer.rb
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Classes: AmbiguousCollectionError, Error, InvalidIncludeError
Constant Summary
collapse
- VERSION =
'0.2.3'
Class Method Summary
collapse
Class Method Details
.find_recursive_relationships(root_object, root_inclusion_tree, results) ⇒ Object
Recursively find object relationships and returns a tree of related objects. Example return:
['comments', '1'] => {object: <>, include_linkages: ['author'],
['users', '1'] => <User>, include_linkages: [],
['users', '2'] => <User>, include_linkages: [],
}
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
# File 'lib/jsonapi-serializers/serializer.rb', line 329
def self.find_recursive_relationships(root_object, root_inclusion_tree, results)
root_inclusion_tree.each do |attribute_name, child_inclusion_tree|
next if attribute_name == :_include
serializer = JSONAPI::Serializer.find_serializer(root_object)
unformatted_attr_name = serializer.unformat_name(attribute_name).to_sym
object = nil
is_collection = false
is_valid_attr = false
if serializer.has_one_relationships.has_key?(unformatted_attr_name)
is_valid_attr = true
object = serializer.has_one_relationships[unformatted_attr_name]
elsif serializer.has_many_relationships.has_key?(unformatted_attr_name)
is_valid_attr = true
is_collection = true
object = serializer.has_many_relationships[unformatted_attr_name]
end
if !is_valid_attr
raise JSONAPI::Serializer::InvalidIncludeError.new(
"'#{attribute_name}' is not a valid include.")
end
next if object.nil?
objects = is_collection ? object : [object]
if child_inclusion_tree[:_include] == true
objects.each do |obj|
obj_serializer = JSONAPI::Serializer.find_serializer(obj)
key = [obj_serializer.type, obj_serializer.id]
current_child_includes = []
inclusion_names = child_inclusion_tree.keys.reject { |k| k == :_include }
inclusion_names.each do |inclusion_name|
if child_inclusion_tree[inclusion_name][:_include]
current_child_includes << inclusion_name
end
end
current_child_includes += results[key] && results[key][:include_linkages] || []
current_child_includes.uniq!
results[key] = {object: obj, include_linkages: current_child_includes}
end
end
if !child_inclusion_tree.empty?
objects.each do |obj|
find_recursive_relationships(obj, child_inclusion_tree, results)
end
end
end
nil
end
|
.find_serializer(object) ⇒ Object
202
203
204
|
# File 'lib/jsonapi-serializers/serializer.rb', line 202
def self.find_serializer(object)
find_serializer_class(object).new(object)
end
|
.find_serializer_class(object) ⇒ Object
197
198
199
200
|
# File 'lib/jsonapi-serializers/serializer.rb', line 197
def self.find_serializer_class(object)
class_name = find_serializer_class_name(object)
class_name.constantize
end
|
.find_serializer_class_name(object) ⇒ Object
193
194
195
|
# File 'lib/jsonapi-serializers/serializer.rb', line 193
def self.find_serializer_class_name(object)
"#{object.class.name}Serializer"
end
|
.included(target) ⇒ Object
6
7
8
9
10
11
12
|
# File 'lib/jsonapi-serializers/serializer.rb', line 6
def self.included(target)
target.send(:include, InstanceMethods)
target.extend ClassMethods
target.class_eval do
include JSONAPI::Attributes
end
end
|
.merge_relationship_path(path, data) ⇒ Object
428
429
430
431
432
433
434
435
436
437
438
439
440
|
# File 'lib/jsonapi-serializers/serializer.rb', line 428
def self.merge_relationship_path(path, data)
parts = path.split('.', 2)
current_level = parts[0].strip
data[current_level] ||= {}
if parts.length == 1
data[current_level].merge!({_include: true})
elsif parts.length == 2
merge_relationship_path(parts[1], data[current_level])
end
end
|
.parse_relationship_paths(paths) ⇒ Object
Takes a list of relationship paths and returns a hash as deep as the given paths. The _include: true is a sentinal value that specifies whether the parent level should be included.
Example:
Given: ['author', 'comments', 'comments.user']
Returns: {
'author' => {_include: true},
'comments' => {_include: true, 'user' => {_include: true}},
}
421
422
423
424
425
|
# File 'lib/jsonapi-serializers/serializer.rb', line 421
def self.parse_relationship_paths(paths)
relationships = {}
paths.each { |path| merge_relationship_path(path, relationships) }
relationships
end
|
.serialize(objects, options = {}) ⇒ Object
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
# File 'lib/jsonapi-serializers/serializer.rb', line 206
def self.serialize(objects, options = {})
options[:is_collection] = options.delete('is_collection') || options[:is_collection] || false
options[:include] = options.delete('include') || options[:include]
options[:serializer] = options.delete('serializer') || options[:serializer]
options[:context] = options.delete('context') || options[:context] || {}
includes = options[:include]
includes = (includes.is_a?(String) ? includes.split(',') : includes).uniq if includes
passthrough_options = {
context: options[:context],
serializer: options[:serializer],
include: includes
}
if options[:is_collection] && !objects.respond_to?(:each)
raise JSONAPI::Serializer::AmbiguousCollectionError.new(
'Attempted to serialize a single object as a collection.')
end
if includes
direct_children_includes = includes.reject { |key| key.include?('.') }
passthrough_options[:include_linkages] = direct_children_includes
end
if options[:is_collection] && !objects.any?
primary_data = []
elsif !options[:is_collection] && objects.nil?
primary_data = nil
elsif options[:is_collection]
passthrough_options[:serializer] ||= find_serializer_class(objects.first)
primary_data = serialize_primary_multi(objects, passthrough_options)
else
if objects.respond_to?(:each)
raise JSONAPI::Serializer::AmbiguousCollectionError.new(
'Must provide `is_collection: true` to `serialize` when serializing collections.')
end
passthrough_options[:serializer] ||= find_serializer_class(objects)
primary_data = serialize_primary(objects, passthrough_options)
end
result = {
'data' => primary_data,
}
if includes
relationship_data = {}
inclusion_tree = parse_relationship_paths(includes)
objects = options[:is_collection] ? objects.to_a : [objects]
objects.compact.each do |obj|
find_recursive_relationships(obj, inclusion_tree, relationship_data)
end
result['included'] = relationship_data.map do |_, data|
passthrough_options = {}
passthrough_options[:serializer] = find_serializer_class(data[:object])
passthrough_options[:include_linkages] = data[:include_linkages]
serialize_primary(data[:object], passthrough_options)
end
end
result
end
|
.serialize_primary(object, options = {}) ⇒ Object
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
# File 'lib/jsonapi-serializers/serializer.rb', line 287
def self.serialize_primary(object, options = {})
serializer_class = options.fetch(:serializer)
return if object.nil?
serializer = serializer_class.new(object, options)
data = {
'id' => serializer.id.to_s,
'type' => serializer.type.to_s,
'attributes' => serializer.attributes,
}
data.merge!({'attributes' => serializer.attributes}) if !serializer.attributes.nil?
data.merge!({'links' => serializer.links}) if !serializer.links.nil?
data.merge!({'relationships' => serializer.relationships}) unless serializer.relationships.empty?
data.merge!({'meta' => serializer.meta}) if !serializer.meta.nil?
data
end
|
.serialize_primary_multi(objects, options = {}) ⇒ Object
312
313
314
315
316
317
318
319
|
# File 'lib/jsonapi-serializers/serializer.rb', line 312
def self.serialize_primary_multi(objects, options = {})
return [] if !objects.any?
objects.map { |obj| serialize_primary(obj, options) }
end
|