Class: MotionJsonApi::Resource

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, top_level = [], included = []) ⇒ Resource

Returns a new instance of Resource.



11
12
13
14
15
16
17
18
19
# File 'lib/motion-json-api/resource.rb', line 11

def initialize(object, top_level = [], included = [])
  @id = object["data"]["id"]
  @attributes = object["data"]["attributes"]
  @relationships = object["data"]["relationships"]
  @meta = object.fetch("meta", {})
  @links = object.fetch("links", {})
  @top_level = top_level
  @included = included
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/motion-json-api/resource.rb', line 4

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/motion-json-api/resource.rb', line 3

def id
  @id
end

#includedObject

Returns the value of attribute included.



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

def included
  @included
end

Returns the value of attribute links.



7
8
9
# File 'lib/motion-json-api/resource.rb', line 7

def links
  @links
end

#metaObject

Returns the value of attribute meta.



6
7
8
# File 'lib/motion-json-api/resource.rb', line 6

def meta
  @meta
end

#relationshipsObject

Returns the value of attribute relationships.



5
6
7
# File 'lib/motion-json-api/resource.rb', line 5

def relationships
  @relationships
end

#top_levelObject

Returns the value of attribute top_level.



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

def top_level
  @top_level
end

Class Method Details

._descendantsObject



80
81
82
# File 'lib/motion-json-api/resource.rb', line 80

def self._descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

._klass_for_type(type) ⇒ Object



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

def self._klass_for_type(type)
  resource_klass = Resource._descendants.select do |klass|
    type == klass._resource_type.to_s
  end.first

  unless resource_klass
    raise UndefinedResource, "Couldn’t find defined resource for type: #{type}"
  end

  resource_klass
end

._object_handler(object, top_level, included = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/motion-json-api/resource.rb', line 84

def self._object_handler(object, top_level, included = nil)
  included ||= self.included || object.fetch("included", [])
  top_level ||= self.top_level || [object["data"]].flatten

  case object["data"]
  when Array
    return object["data"].map do |data|
      resource_klass = Resource._klass_for_type(data["type"])
      resource_klass.new({"data" => data}, top_level, included)
    end
  when Hash
    resource_klass = Resource._klass_for_type(object["data"]["type"])
    resource_klass.new(object, top_level, included)
  end
end

._resource_typeObject



26
27
28
# File 'lib/motion-json-api/resource.rb', line 26

def self._resource_type
  @_resource_type
end

.attribute(attribute) ⇒ Object



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

def self.attribute(attribute)
  define_method(attribute) do
    self.attributes.fetch(attribute.to_s)
  end
end

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



47
48
49
50
51
52
53
54
55
56
# File 'lib/motion-json-api/resource.rb', line 47

def self.has_many(relation, options = {})
  key = options.fetch(:as, relation).to_s
  define_method(key) do
    relationship = self.relationships.fetch(key)
    relationship.fetch("data").map do |data|
      object = _find_in_included(data["id"], data["type"])
      Resource._object_handler({"data" => object}, self.top_level, self.included)
    end
  end
end

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



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

def self.has_one(relation, options = {})
  key = options.fetch(:as, relation).to_s
  define_method(key) do
    relationship = self.relationships.fetch(key)
    data = relationship.fetch("data")
    object = _find_in_included(data["id"], data["type"])
    payload = {"data" => object, "links" => relationship.fetch("links", {})}
    Resource._object_handler(payload, self.top_level, self.included)
  end
end

.resource_type(resource_type) ⇒ Object



22
23
24
# File 'lib/motion-json-api/resource.rb', line 22

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