class JSONAPI::Document::Resource::Relationships::Relationship

The relationships of a resource

Attributes

data[RW]
meta[RW]
name[R]

Public Class Methods

new(rels_member_hash) click to toggle source

@param rels_member_hash [Hash] The hash of relationship members

# 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

Public Instance Methods

to_h() click to toggle source

Hash representation of a relationship

# 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_s() click to toggle source

@return [String] A JSON parseable representation of a relationship

# 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