Class: Jacoat::Document::ResourceIdentifier
- Inherits:
-
Object
- Object
- Jacoat::Document::ResourceIdentifier
- Defined in:
- lib/jacoat/document/resource_identifier.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#relationships ⇒ Object
Returns the value of attribute relationships.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #create_relationships(hash) ⇒ Object
-
#initialize(id, type) ⇒ ResourceIdentifier
constructor
A new instance of ResourceIdentifier.
- #process_jsonapi(arguments) ⇒ Object
- #relationship_hash ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(id, type) ⇒ ResourceIdentifier
Returns a new instance of ResourceIdentifier.
17 18 19 20 |
# File 'lib/jacoat/document/resource_identifier.rb', line 17 def initialize(id, type) @id = id @type = type end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/jacoat/document/resource_identifier.rb', line 4 def id @id end |
#relationships ⇒ Object
Returns the value of attribute relationships.
4 5 6 |
# File 'lib/jacoat/document/resource_identifier.rb', line 4 def relationships @relationships end |
#type ⇒ Object
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
.from_jsonapi(body) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/jacoat/document/resource_identifier.rb', line 5 def self.from_jsonapi(body) if body.is_a?(Array) resources = [] body.each do |item| resources << ResourceIdentifier.new(item[:id], item[:type]).process_jsonapi(item) end else resources = ResourceIdentifier.new(body[:id], body[:type]).process_jsonapi(body) end resources end |
Instance Method Details
#create_relationships(hash) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/jacoat/document/resource_identifier.rb', line 27 def create_relationships(hash) @relationships = [] hash.each_pair do |k, v| @relationships << Document::Relationship.new(k).process_jsonapi(v) end end |
#process_jsonapi(arguments) ⇒ Object
22 23 24 25 |
# File 'lib/jacoat/document/resource_identifier.rb', line 22 def process_jsonapi(arguments) create_relationships(arguments[:relationships]) if arguments.has_key?(:relationships) self end |
#relationship_hash ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/jacoat/document/resource_identifier.rb', line 43 def relationship_hash hash = {} @relationships.each do |relationship| hash.merge!(relationship.to_hash) end hash end |
#to_hash ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/jacoat/document/resource_identifier.rb', line 34 def to_hash hash = { type: @type, id: @id } hash.merge!(relationships: relationship_hash) if @relationships hash end |