module Quiver::Serialization::JsonApi::Serializer

Attributes

collection_info[RW]

Public Class Methods

included(base) click to toggle source
# File lib/quiver/serialization/json_api/serializer.rb, line 4
def self.included(base)
  base.send(:extend, ClassMethods)
end
new(collection_info) click to toggle source
# File lib/quiver/serialization/json_api/serializer.rb, line 10
def initialize(collection_info)
  self.collection_info = collection_info
end

Public Instance Methods

serialize(opts={}) click to toggle source
# File lib/quiver/serialization/json_api/serializer.rb, line 14
def serialize(opts={})
  output = {}

  [:data, :linked, :errors].each do |type|
    if collection = fetch_collection(type)
      output[type] = serialize_items(collection, opts)
    end
  end

  output
end

Private Instance Methods

fetch_collection(key) click to toggle source
# File lib/quiver/serialization/json_api/serializer.rb, line 28
def fetch_collection(key)
  collection_info[:collections][key]
end
serialize_items(items, opts) click to toggle source
# File lib/quiver/serialization/json_api/serializer.rb, line 32
def serialize_items(items, opts)
  items.map do |item|
    serialization_type = if item.respond_to?(:serialization_type)
      item.serialization_type
    else
      item.class.name
    end

    if handler = self.class.type_handlers[serialization_type]
      handler.serialize(item, opts).merge(
        type: serialization_type.underscore.pluralize
      )
    end
  end.compact
end