Class: Jacoat::Document::Relationship

Inherits:
Object
  • Object
show all
Defined in:
lib/jacoat/document/relationship.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Relationship

Returns a new instance of Relationship.



15
16
17
# File 'lib/jacoat/document/relationship.rb', line 15

def initialize(type)
  @type = type.to_s
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/jacoat/document/relationship.rb', line 4

def data
  @data
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/jacoat/document/relationship.rb', line 4

def id
  @id
end

Returns the value of attribute links.



4
5
6
# File 'lib/jacoat/document/relationship.rb', line 4

def links
  @links
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/jacoat/document/relationship.rb', line 4

def type
  @type
end

Class Method Details

.from_jsonapi(hash) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/jacoat/document/relationship.rb', line 5

def self.from_jsonapi(hash)
  relationships = []
  hash.each_pair do |k, v|
    relationship = Document::Relationship.new(k)
    relationship.process_jsonapi(v)
    relationships << relationship
  end
  relationships
end

Instance Method Details

#process_jsonapi(hash) ⇒ Object



27
28
29
30
31
# File 'lib/jacoat/document/relationship.rb', line 27

def process_jsonapi(hash)
  process_data(hash[:data]) if hash.has_key?(:data)
  process_body(hash[:links]) if hash.has_key?(:links)
  self
end

#to_hashObject



19
20
21
22
23
24
25
# File 'lib/jacoat/document/relationship.rb', line 19

def to_hash
  hash = {}
  hash[type.to_sym] = {}
  hash[type.to_sym].merge!(links: @links.to_hash) if @links
  hash[type.to_sym].merge!(data: data_to_hash) if @data
  hash
end