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

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

Overview

The relationships of a resource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rels_member_hash) ⇒ Relationship

Returns a new instance of Relationship.

Parameters:

  • rels_member_hash (Hash)

    The hash of relationship members



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/easy/jsonapi/document/resource/relationships/relationship.rb', line 16

def initialize(rels_member_hash)
  unless rels_member_hash.is_a? Hash
    raise 'Must initialize a ' \
          'JSONAPI::Document::Resource::Relationships::Relationship with a Hash'
  end
  # TODO: Knowing whether a relationship is to-one or to-many can assist in validating
  #   compliance and cross checking a document.
  @name = rels_member_hash[:name].to_s
  @links = rels_member_hash[:links]
  @data = rels_member_hash[:data]
  @meta = rels_member_hash[:meta]
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



12
13
14
# File 'lib/easy/jsonapi/document/resource/relationships/relationship.rb', line 12

def data
  @data
end

Returns the value of attribute links.



12
13
14
# File 'lib/easy/jsonapi/document/resource/relationships/relationship.rb', line 12

def links
  @links
end

#metaObject

Returns the value of attribute meta.



12
13
14
# File 'lib/easy/jsonapi/document/resource/relationships/relationship.rb', line 12

def meta
  @meta
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/easy/jsonapi/document/resource/relationships/relationship.rb', line 13

def name
  @name
end

Instance Method Details

#to_hObject

Hash representation of a relationship



39
40
41
42
43
44
45
# File 'lib/easy/jsonapi/document/resource/relationships/relationship.rb', line 39

def to_h
  { @name.to_sym => {
    links: @links.to_h,
    data: @data.to_h,
    meta: @meta.to_h
  } }
end

#to_sString

Returns A JSON parseable representation of a relationship.

Returns:

  • (String)

    A JSON parseable representation of a relationship



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

def to_s
  "\"#{@name}\": { " \
    "#{JSONAPI::Utility.member_to_s('links', @links, first_member: true)}" \
    "#{JSONAPI::Utility.member_to_s('data', @data)}" \
    "#{JSONAPI::Utility.member_to_s('meta', @meta)}" \
  ' }' \
end