Class: Subjoin::Resource
- Inherits:
-
Object
- Object
- Subjoin::Resource
- Includes:
- Attributable, Linkable, Metable
- Defined in:
- lib/subjoin/resource.rb
Overview
A JSON-API Resource object
Instance Attribute Summary collapse
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#relationships ⇒ Hash<Relationship>
readonly
The relationships specified for the object.
Attributes included from Metable
Attributes included from Linkable
Attributes included from Attributable
Instance Method Summary collapse
-
#id ⇒ String
Resource id.
-
#initialize(spec, doc = nil) ⇒ Resource
constructor
A new instance of Resource.
-
#rels(spec = nil, doc = @document) ⇒ Hash, ...
Get a related resource or resources.
-
#type ⇒ String
Resource type.
Methods included from Metable
Methods included from Linkable
Methods included from Attributable
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 = (data['meta']) end |
Instance Attribute Details
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
14 15 16 |
# File 'lib/subjoin/resource.rb', line 14 def identifier @identifier end |
#relationships ⇒ Hash<Relationship> (readonly)
The relationships specified for the object
12 13 14 |
# File 'lib/subjoin/resource.rb', line 12 def relationships @relationships end |
Instance Method Details
#id ⇒ String
Resource id
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
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 |
#type ⇒ String
Resource type
42 43 44 |
# File 'lib/subjoin/resource.rb', line 42 def type @identifier.type end |