Class: Jacoat::Document::ResourceIdentifier

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ ResourceIdentifier

Returns a new instance of ResourceIdentifier.



17
18
19
20
21
# File 'lib/jacoat/document/resource_identifier.rb', line 17

def initialize(arguments)
  @id = arguments[:id]
  @type = arguments[:type]
  create_relationships(arguments[:relationships]) if arguments.has_key?(:relationships)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



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

def relationships
  @relationships
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.process(body) ⇒ Object



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

def self.process(body)
  if body.is_a?(Array)
    resources = []
    body.each do |item|
      resources << ResourceIdentifier.new(item)
    end
  else
    resources = ResourceIdentifier.new(body)
  end
  resources
end

Instance Method Details

#create_relationships(hash) ⇒ Object



23
24
25
26
27
28
# File 'lib/jacoat/document/resource_identifier.rb', line 23

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

#relationship_hashObject



39
40
41
42
43
44
45
# File 'lib/jacoat/document/resource_identifier.rb', line 39

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

#to_hashObject



30
31
32
33
34
35
36
37
# File 'lib/jacoat/document/resource_identifier.rb', line 30

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