Class: Jacoat::Document::Resource
- Inherits:
-
Object
- Object
- Jacoat::Document::Resource
- Defined in:
- lib/jacoat/document/resource.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#id ⇒ Object
Returns the value of attribute id.
-
#links ⇒ Object
Returns the value of attribute links.
-
#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) ⇒ Resource
constructor
A new instance of Resource.
- #relationship_hash ⇒ Object
- #to_hash ⇒ Object
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
#attributes ⇒ Object
Returns the value of attribute attributes.
4 5 6 |
# File 'lib/jacoat/document/resource.rb', line 4 def attributes @attributes end |
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/jacoat/document/resource.rb', line 4 def id @id end |
#links ⇒ Object
Returns the value of attribute links.
4 5 6 |
# File 'lib/jacoat/document/resource.rb', line 4 def links @links end |
#relationships ⇒ Object
Returns the value of attribute relationships.
4 5 6 |
# File 'lib/jacoat/document/resource.rb', line 4 def relationships @relationships end |
#type ⇒ Object
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_hash ⇒ Object
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_hash ⇒ Object
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 |