Class: Jacoat::Document::Resource

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, type) ⇒ Resource

Returns a new instance of Resource.



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

def initialize(id, type)
  @id = id
  @type = type
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

Returns the value of attribute links.



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

def links
  @links
end

#relationshipsObject

Returns the value of attribute relationships.



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

def relationships
  @relationships
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.from_jsonapi(arguments) ⇒ Object



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

def self.from_jsonapi(arguments)
  resource = Resource.new(arguments[:id], arguments[:type])
  resource.attributes = Document::Attributes.from_jsonapi(arguments[:attributes])
  resource.create_relationships(arguments[:relationships]) if arguments.has_key?(:relationships)
  resource.links = Link.from_jsonapi(arguments[:links]) if arguments.has_key?(:links)
  resource
end

Instance Method Details

#create_relationships(hash) ⇒ Object



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

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

#relationship_hashObject



37
38
39
40
41
42
43
# File 'lib/jacoat/document/resource.rb', line 37

def relationship_hash
  hash = {}
  @relationships.each do |relationship|
    hash.merge!(relationship.to_hash)
  end
  hash
end

#to_hashObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/jacoat/document/resource.rb', line 26

def to_hash
  hash = {
    type: @type,
    id: @id.to_s,
    attributes: @attributes.to_hash
  }
  hash.merge!(links: @links.to_hash) if @links
  hash.merge!(relationships: relationship_hash) if @relationships
  hash
end