Sha256: 2614ac75c30616c74d78477b79a6e68d050219b84cc86b77006cf436cff43a33

Contents?: true

Size: 951 Bytes

Versions: 1

Compression:

Stored size: 951 Bytes

Contents

module Subjoin
  # A related resource link, providing access to resource objects
  # linked in a relationship
  # @see http://jsonapi.org/format/#document-resource-object-related-resource-links
  class Relationship
    include Linkable
    include Metable

    attr_reader :links, :linkages
    def initialize(data, doc)
      @document = doc
      @links = load_links(data['links'])
      @linkages = load_linkages(data['data'], doc)
      @meta = load_meta(data['meta'])
    end

    # Resolve available linkages and return related resources
    # @return [Array<Subjoin::Resource>]
    def lookup
      return [] unless @document.has_included?
      @linkages.map{|l| @document.included[l]}
    end

    private
    def load_linkages(data, doc)
      return [] if data.nil?
      return [Identifier.new(data['type'], data['id'], data['meta'])] if data.is_a? Hash
      data.map{|l| Identifier.new(l['type'], l['id'], l['meta'])}
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
subjoin-0.2.1 lib/subjoin/relationship.rb