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.5.1 lib/dato/json_api_serializer.rb
dato-0.5.0 lib/dato/json_api_serializer.rb
dato-0.4.3 lib/dato/json_api_serializer.rb
dato-0.4.2 lib/dato/json_api_serializer.rb
dato-0.4.1 lib/dato/json_api_serializer.rb
dato-0.4.0 lib/dato/json_api_serializer.rb
dato-0.3.31 lib/dato/json_api_serializer.rb
dato-0.4.0.pre lib/dato/json_api_serializer.rb
dato-0.3.30 lib/dato/json_api_serializer.rb
dato-0.3.29 lib/dato/json_api_serializer.rb
dato-0.3.28 lib/dato/json_api_serializer.rb
dato-0.3.27 lib/dato/json_api_serializer.rb
dato-0.3.26 lib/dato/json_api_serializer.rb
dato-0.3.25 lib/dato/json_api_serializer.rb
dato-0.3.24 lib/dato/json_api_serializer.rb
dato-0.3.23 lib/dato/json_api_serializer.rb
dato-0.3.22 lib/dato/json_api_serializer.rb
dato-0.3.21 lib/dato/json_api_serializer.rb
dato-0.3.20 lib/dato/json_api_serializer.rb
dato-0.3.19 lib/dato/json_api_serializer.rb