Class: JSONAPI::Document::Resource::Relationships

Inherits:
NameValuePairCollection show all
Defined in:
lib/easy/jsonapi/document/resource/relationships.rb,
lib/easy/jsonapi/document/resource/relationships/relationship.rb

Overview

A JSONAPI resource’s relationships

Defined Under Namespace

Classes: Relationship

Instance Method Summary collapse

Methods inherited from NameValuePairCollection

#<<, #to_s

Methods inherited from Collection

#[], #[]=, #each, #empty?, #get, #include?, #insert, #keys, #remove, #set, #size, #to_s

Constructor Details

#initialize(rels_obj_arr = []) ⇒ Relationships

Returns a new instance of Relationships.

Parameters:



15
16
17
# File 'lib/easy/jsonapi/document/resource/relationships.rb', line 15

def initialize(rels_obj_arr = [])
  super(rels_obj_arr, item_type: JSONAPI::Document::Resource::Relationships::Relationship)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class JSONAPI::NameValuePairCollection

Instance Method Details

#add(relationship) ⇒ Object

Add a jsonapi member to the collection

Parameters:



21
22
23
# File 'lib/easy/jsonapi/document/resource/relationships.rb', line 21

def add(relationship)
  super(relationship, &:name)
end

#to_hHash

The jsonapi hash representation of a resource’s relationships

Returns:

  • (Hash)

    A resource’s relationships



27
28
29
30
31
32
33
34
35
36
# File 'lib/easy/jsonapi/document/resource/relationships.rb', line 27

def to_h
  to_return = {}
  each do |rel|
    to_return[rel.name.to_sym] = {}
    JSONAPI::Utility.to_h_member(to_return[rel.name.to_sym], rel.links, :links)
    JSONAPI::Utility.to_h_member(to_return[rel.name.to_sym], rel.data, :data)
    JSONAPI::Utility.to_h_member(to_return[rel.name.to_sym], rel.meta, :meta)
  end
  to_return
end