Class: MotionJsonApi::Resource
- Inherits:
-
Object
- Object
- MotionJsonApi::Resource
- Extended by:
- Descendants
- Defined in:
- lib/motion-json-api/resource.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#id ⇒ Object
Returns the value of attribute id.
-
#included ⇒ Object
Returns the value of attribute included.
-
#links ⇒ Object
Returns the value of attribute links.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#relationships ⇒ Object
Returns the value of attribute relationships.
-
#top_level ⇒ Object
Returns the value of attribute top_level.
Class Method Summary collapse
- ._resource_type ⇒ Object
- .attribute(attribute, options = {}) ⇒ Object
- .has_many(relation, options = {}) ⇒ Object
- .has_one(relation, options = {}) ⇒ Object
- .resource_type(resource_type) ⇒ Object
Instance Method Summary collapse
-
#initialize(object, top_level = [], included = []) ⇒ Resource
constructor
A new instance of Resource.
Methods included from Descendants
clear, descendants, descendants, direct_descendants, direct_descendants, inherited, store_inherited
Constructor Details
#initialize(object, top_level = [], included = []) ⇒ Resource
Returns a new instance of Resource.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/motion-json-api/resource.rb', line 17 def initialize(object, top_level = [], included = []) @id = object["data"]["id"] @attributes = object["data"].fetch("attributes", {}) @relationships = object["data"].fetch("relationships", {}) @meta = object.fetch("meta") do object["data"].fetch("meta", {}) end @links = object.fetch("links") do object["data"].fetch("links", {}) end @top_level = top_level @included = included end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
10 11 12 |
# File 'lib/motion-json-api/resource.rb', line 10 def attributes @attributes end |
#id ⇒ Object
Returns the value of attribute id.
9 10 11 |
# File 'lib/motion-json-api/resource.rb', line 9 def id @id end |
#included ⇒ Object
Returns the value of attribute included.
15 16 17 |
# File 'lib/motion-json-api/resource.rb', line 15 def included @included end |
#links ⇒ Object
Returns the value of attribute links.
13 14 15 |
# File 'lib/motion-json-api/resource.rb', line 13 def links @links end |
#meta ⇒ Object
Returns the value of attribute meta.
12 13 14 |
# File 'lib/motion-json-api/resource.rb', line 12 def @meta end |
#relationships ⇒ Object
Returns the value of attribute relationships.
11 12 13 |
# File 'lib/motion-json-api/resource.rb', line 11 def relationships @relationships end |
#top_level ⇒ Object
Returns the value of attribute top_level.
14 15 16 |
# File 'lib/motion-json-api/resource.rb', line 14 def top_level @top_level end |
Class Method Details
._resource_type ⇒ Object
36 37 38 |
# File 'lib/motion-json-api/resource.rb', line 36 def self._resource_type @_resource_type end |
.attribute(attribute, options = {}) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/motion-json-api/resource.rb', line 40 def self.attribute(attribute, = {}) key = .fetch(:key, attribute) define_method(key) do self.attributes.fetch(attribute.to_s, nil) end end |
.has_many(relation, options = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/motion-json-api/resource.rb', line 67 def self.has_many(relation, = {}) key = .fetch(:key, relation) define_method(key) do relationship = self.relationships.fetch(relation.to_s, {}) relationship.fetch("data", []).map do |data| object = _find_in_included(data["id"], data["type"]) if object Resource._object_handler({"data" => object}, self.top_level, self.included) else Resource._object_handler({"data" => data}, self.top_level, self.included) end end end end |
.has_one(relation, options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/motion-json-api/resource.rb', line 47 def self.has_one(relation, = {}) key = .fetch(:key, relation) define_method(key) do relationship = self.relationships.fetch(relation.to_s, {}) data = relationship.fetch("data", nil) if data object = _find_in_included(data["id"], data["type"]) if object payload = {"data" => object, "links" => relationship.fetch("links", {})} Resource._object_handler(payload, self.top_level, self.included) else {"id" => data["id"], "type" => data["type"]} end else nil end end end |
.resource_type(resource_type) ⇒ Object
32 33 34 |
# File 'lib/motion-json-api/resource.rb', line 32 def self.resource_type(resource_type) @_resource_type = resource_type end |