Class: MotionJsonApi::Resource

Inherits:
Object
  • Object
show all
Extended by:
Descendants
Defined in:
lib/motion-json-api/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
# 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", {})
  @top_level = top_level
  @included = included
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



10
11
12
# File 'lib/motion-json-api/resource.rb', line 10

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/motion-json-api/resource.rb', line 9

def id
  @id
end

#includedObject

Returns the value of attribute included.



15
16
17
# File 'lib/motion-json-api/resource.rb', line 15

def included
  @included
end

Returns the value of attribute links.



13
14
15
# File 'lib/motion-json-api/resource.rb', line 13

def links
  @links
end

#metaObject

Returns the value of attribute meta.



12
13
14
# File 'lib/motion-json-api/resource.rb', line 12

def meta
  @meta
end

#relationshipsObject

Returns the value of attribute relationships.



11
12
13
# File 'lib/motion-json-api/resource.rb', line 11

def relationships
  @relationships
end

#top_levelObject

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_typeObject



34
35
36
# File 'lib/motion-json-api/resource.rb', line 34

def self._resource_type
  @_resource_type
end

.attribute(attribute, options = {}) ⇒ Object



38
39
40
41
42
43
# File 'lib/motion-json-api/resource.rb', line 38

def self.attribute(attribute, options = {})
  key = options.fetch(:key, attribute)
  define_method(key) do
    self.attributes.fetch(attribute.to_s, nil)
  end
end

.has_many(relation, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/motion-json-api/resource.rb', line 65

def self.has_many(relation, options = {})
  key = options.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
        {"id" => data["id"], "type" => data["type"]}
      end
    end
  end
end

.has_one(relation, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/motion-json-api/resource.rb', line 45

def self.has_one(relation, options = {})
  key = options.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



30
31
32
# File 'lib/motion-json-api/resource.rb', line 30

def self.resource_type(resource_type)
  @_resource_type = resource_type
end