Class: JSONAPI::Document::Resource::Relationships::Relationship
- Inherits:
-
Object
- Object
- JSONAPI::Document::Resource::Relationships::Relationship
- Defined in:
- lib/easy/jsonapi/document/resource/relationships/relationship.rb
Overview
The relationships of a resource
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#links ⇒ Object
Returns the value of attribute links.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(rels_member_hash) ⇒ Relationship
constructor
A new instance of Relationship.
-
#to_h ⇒ Object
Hash representation of a relationship.
-
#to_s ⇒ String
A JSON parseable representation of a relationship.
Constructor Details
#initialize(rels_member_hash) ⇒ Relationship
Returns a new instance of Relationship.
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
#data ⇒ Object
Returns the value of attribute data.
12 13 14 |
# File 'lib/easy/jsonapi/document/resource/relationships/relationship.rb', line 12 def data @data end |
#links ⇒ Object
Returns the value of attribute links.
12 13 14 |
# File 'lib/easy/jsonapi/document/resource/relationships/relationship.rb', line 12 def links @links end |
#meta ⇒ Object
Returns the value of attribute meta.
12 13 14 |
# File 'lib/easy/jsonapi/document/resource/relationships/relationship.rb', line 12 def @meta end |
#name ⇒ Object (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_h ⇒ Object
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_s ⇒ String
Returns 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 |