Module: JsonApi::Resources::Base
- Included in:
- JsonApi::Resource
- Defined in:
- lib/json_api_ruby/resources/base.rb
Instance Attribute Summary collapse
-
#relationships ⇒ Object
readonly
Returns the value of attribute relationships.
Instance Method Summary collapse
- #attributes_hash ⇒ Object
-
#build_object_graph ⇒ Object
Builds relationship resource classes.
- #identifier_hash ⇒ Object
- #links_hash ⇒ Object
-
#parse_for_includes(includes) ⇒ Object
Very basic.
- #self_link_path ⇒ Object
- #to_hash(options = {}) ⇒ Object
Instance Attribute Details
#relationships ⇒ Object (readonly)
Returns the value of attribute relationships.
6 7 8 |
# File 'lib/json_api_ruby/resources/base.rb', line 6 def relationships @relationships end |
Instance Method Details
#attributes_hash ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/json_api_ruby/resources/base.rb', line 41 def attributes_hash attrs = {} Array(self.class.fields).each do |attr| attrs[attr.to_s] = send(attr) end attrs end |
#build_object_graph ⇒ Object
Builds relationship resource classes
50 51 52 53 54 55 56 57 |
# File 'lib/json_api_ruby/resources/base.rb', line 50 def build_object_graph @relationships ||= [] Array(self.class.relationships).each do |relationship| included = includes.include?(relationship.name) rel = relationship.build_resources({parent_resource: self, included: included}) @relationships << rel end end |
#identifier_hash ⇒ Object
29 30 31 |
# File 'lib/json_api_ruby/resources/base.rb', line 29 def identifier_hash { 'id' => self.id, 'type' => self.type } end |
#links_hash ⇒ Object
33 34 35 |
# File 'lib/json_api_ruby/resources/base.rb', line 33 def links_hash { 'self' => JsonApi.configuration.base_url + self_link_path } end |
#parse_for_includes(includes) ⇒ Object
Very basic. Eventually this will need to parse things like “article.comments” and “article-comments”, so, leaving the method here but only supporting the most basic of things
25 26 27 |
# File 'lib/json_api_ruby/resources/base.rb', line 25 def parse_for_includes(includes) Array(includes).map {|inc| inc.to_s } end |
#self_link_path ⇒ Object
37 38 39 |
# File 'lib/json_api_ruby/resources/base.rb', line 37 def self_link_path "/#{self.type}/#{self.id}" end |
#to_hash(options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/json_api_ruby/resources/base.rb', line 7 def to_hash(={}) .symbolize_keys resource_hash = identifier_hash resource_hash['attributes'] = attributes_hash if attributes_hash.any? relationships.each do |relationship| resource_hash['relationships'] ||= {} resource_hash['relationships'][relationship.name] = relationship.to_hash end resource_hash['links'] = links_hash resource_hash end |