Sha256: 8b35a6f6acd893c3b56d85b7e1cfa9f7b06ac806ec0183e89db304527016a782

Contents?: true

Size: 1.92 KB

Versions: 67

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true
module Dato
  class JsonApiSerializer
    attr_reader :type, :attributes, :relationships
    attr_reader :required_attributes, :required_relationships

    def initialize(
      type:,
      attributes: [],
      required_attributes: [],
      relationships: {},
      required_relationships: []
    )
      @type = type
      @attributes = attributes
      @required_attributes = required_attributes
      @relationships = relationships
      @required_relationships = required_relationships
    end

    def serialize(resource, id = nil)
      resource = resource.with_indifferent_access
      data = {}

      data[:id] = id || resource[:id] if id || resource[:id]

      data[:type] = type
      data[:attributes] = serialized_attributes(resource)

      if relationships.any?
        data[:relationships] = serialized_relationships(resource)
      end

      { data: data }
    end

    def serialized_attributes(resource)
      result = {}

      attributes.each do |attribute|
        if resource.key? attribute
          result[attribute] = resource[attribute]
        elsif required_attributes.include? attribute
          throw "Required attribute: #{attribute}"
        end
      end

      result
    end

    def serialized_relationships(resource)
      result = {}

      relationships.each do |relationship, meta|
        if resource.key? relationship
          value = resource[relationship]

          data = if value
                   if meta[:collection]
                     value.map do |id|
                       { type: meta[:type], id: id.to_s }
                     end
                   else
                     { type: meta[:type], id: value.to_s }
                   end
                 end
          result[relationship] = { data: data }

        elsif required_relationships.include? relationship
          throw "Required attribute: #{relationship}"
        end
      end

      result
    end
  end
end

Version data entries

67 entries across 67 versions & 1 rubygems

Version Path
dato-0.2.6 lib/dato/json_api_serializer.rb
dato-0.2.4 lib/dato/json_api_serializer.rb
dato-0.1.31 lib/dato/json_api_serializer.rb
dato-0.1.30 lib/dato/json_api_serializer.rb
dato-0.1.29 lib/dato/json_api_serializer.rb
dato-0.1.28 lib/dato/json_api_serializer.rb
dato-0.1.27 lib/dato/json_api_serializer.rb
dato-0.1.26 lib/dato/json_api_serializer.rb
dato-0.1.25 lib/dato/json_api_serializer.rb
dato-0.1.24 lib/dato/json_api_serializer.rb
dato-0.1.23 lib/dato/json_api_serializer.rb
dato-0.1.22 lib/dato/json_api_serializer.rb
dato-0.1.21 lib/dato/json_api_serializer.rb
dato-0.1.20 lib/dato/json_api_serializer.rb
dato-0.1.19 lib/dato/json_api_serializer.rb
dato-0.1.18 lib/dato/json_api_serializer.rb
dato-0.1.17 lib/dato/json_api_serializer.rb
dato-0.1.16 lib/dato/json_api_serializer.rb
dato-0.1.15 lib/dato/json_api_serializer.rb
dato-0.1.14 lib/dato/json_api_serializer.rb