Class: JSONAPI::Document::Resource
- Inherits:
-
Object
- Object
- JSONAPI::Document::Resource
- Defined in:
- lib/easy/jsonapi/document/resource.rb,
lib/easy/jsonapi/document/resource/attributes.rb,
lib/easy/jsonapi/document/resource/relationships.rb,
lib/easy/jsonapi/document/resource/attributes/attribute.rb,
lib/easy/jsonapi/document/resource/relationships/relationship.rb
Overview
A jsonapi resource object
Defined Under Namespace
Classes: Attributes, Relationships
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.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#relationships ⇒ Object
Returns the value of attribute relationships.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(members_hash) ⇒ Resource
constructor
A new instance of Resource.
-
#to_h ⇒ Hash
Hash representation of a jsonapi resource.
-
#to_s ⇒ String
String representation of Document that is JSON parsable If any document memeber is nil, it does not include it in the returned string.
Constructor Details
#initialize(members_hash) ⇒ Resource
Returns a new instance of Resource.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/easy/jsonapi/document/resource.rb', line 15 def initialize(members_hash) unless members_hash.is_a? Hash raise 'A JSONAPI::Document::Resource must be initialized with a Hash' end @type = members_hash[:type].to_s unless members_hash[:type].nil? @id = members_hash[:id].to_s unless members_hash[:id].nil? @attributes = members_hash[:attributes] @relationships = members_hash[:relationships] @links = members_hash[:links] @meta = members_hash[:meta] end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
12 13 14 |
# File 'lib/easy/jsonapi/document/resource.rb', line 12 def attributes @attributes end |
#id ⇒ Object
Returns the value of attribute id.
12 13 14 |
# File 'lib/easy/jsonapi/document/resource.rb', line 12 def id @id end |
#links ⇒ Object
Returns the value of attribute links.
12 13 14 |
# File 'lib/easy/jsonapi/document/resource.rb', line 12 def links @links end |
#meta ⇒ Object
Returns the value of attribute meta.
12 13 14 |
# File 'lib/easy/jsonapi/document/resource.rb', line 12 def @meta end |
#relationships ⇒ Object
Returns the value of attribute relationships.
12 13 14 |
# File 'lib/easy/jsonapi/document/resource.rb', line 12 def relationships @relationships end |
#type ⇒ Object
Returns the value of attribute type.
12 13 14 |
# File 'lib/easy/jsonapi/document/resource.rb', line 12 def type @type end |
Instance Method Details
#to_h ⇒ Hash
Hash representation of a jsonapi resource
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/easy/jsonapi/document/resource.rb', line 44 def to_h to_return = {} JSONAPI::Utility.to_h_member(to_return, @type, :type) JSONAPI::Utility.to_h_member(to_return, @id, :id) JSONAPI::Utility.to_h_member(to_return, @attributes, :attributes) JSONAPI::Utility.to_h_member(to_return, @relationships, :relationships) JSONAPI::Utility.to_h_member(to_return, @links, :links) JSONAPI::Utility.to_h_member(to_return, @meta, :meta) to_return end |
#to_s ⇒ String
String representation of Document that is JSON parsable
If any document memeber is nil, it does not include it
in the returned string.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/easy/jsonapi/document/resource.rb', line 31 def to_s '{ ' \ "#{JSONAPI::Utility.member_to_s('type', @type, first_member: true)}" \ "#{JSONAPI::Utility.member_to_s('id', @id)}" \ "#{JSONAPI::Utility.member_to_s('attributes', @attributes)}" \ "#{JSONAPI::Utility.member_to_s('relationships', @relationships)}" \ "#{JSONAPI::Utility.member_to_s('links', @links)}" \ "#{JSONAPI::Utility.member_to_s('meta', @meta)}" \ ' }' end |