Class: Subjoin::Resource

Inherits:
Object
  • Object
show all
Includes:
Attributable, Linkable, Metable
Defined in:
lib/subjoin/resource.rb

Overview

A JSON-API Resource object

Instance Attribute Summary collapse

Attributes included from Metable

#meta

Attributes included from Linkable

#links

Attributes included from Attributable

#attributes

Instance Method Summary collapse

Methods included from Metable

#has_meta?, #load_meta

Methods included from Linkable

#has_links?, #load_links

Methods included from Attributable

#[], #load_attributes

Constructor Details

#initialize(spec, doc = nil) ⇒ Resource

Returns a new instance of Resource.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/subjoin/resource.rb', line 16

def initialize(spec, doc = nil)
  @document = doc
  if spec.is_a?(URI)
    data = Subjoin::get(spec)
  elsif spec.is_a?(Hash)
    data = spec
  end

  if data.has_key?("data")
    data = data["data"]
  end

  if data.is_a?(Array)
    raise UnexpectedTypeError.new
  end

  @identifier = Identifier.new(data['type'], data['id'])
  
  load_attributes(data['attributes'])
  @links = load_links(data['links'])
  @relationships = load_relationships(data['relationships'], @document)
  @meta = load_meta(data['meta'])
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



14
15
16
# File 'lib/subjoin/resource.rb', line 14

def identifier
  @identifier
end

#relationshipsHash<Relationship> (readonly)

The relationships specified for the object

Returns:



12
13
14
# File 'lib/subjoin/resource.rb', line 12

def relationships
  @relationships
end

Instance Method Details

#idString

Resource id

Returns:

  • (String)


48
49
50
# File 'lib/subjoin/resource.rb', line 48

def id
  @identifier.id
end

#rels(spec = nil, doc = @document) ⇒ Hash, ...

Get a related resource or resources. This method resolves the relationship linkages and fetches the included Subjoin::Resource objects themselves. parameter, the return value will be an Array of Subjoin::Resource objects corresponding to the key, or nil if that key doesn’t exist. If called without a spec parameter, the return value will be a Hash whose keys are the same as #relationships, but whose values are Arrays of resolved Subjoin::Resource objects. In practice this means that you have a choice of idioms (method vs. hash) since

obj.rels("key")

and

obj.rels["key"]

are equivalent

Parameters:

  • spec (String) (defaults to: nil)

    key for the desired resource

  • doc (Subjoin::Document) (defaults to: @document)

    Document in which to look for related resources. By default it is the same document from which the resource came itself.

Returns:

  • (Hash, Array<Subjoin:Resource>, nil)

    If called with a spec



74
75
76
77
78
79
80
81
82
83
# File 'lib/subjoin/resource.rb', line 74

def rels(spec = nil, doc = @document)
  return nil if doc.nil?
  return nil unless doc.has_included?

  if spec.nil?
    return Hash[relationships.keys.map{|k| [k, rels(k, doc)]}]
  end
  
  relationships[spec].linkages.map{|l| doc.included[l]}
end

#typeString

Resource type

Returns:

  • (String)


42
43
44
# File 'lib/subjoin/resource.rb', line 42

def type
  @identifier.type
end