Class: SimpleJsonApi::ApiNode

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/simple_json_api/api_node.rb

Overview

Node in the directed graph of associations (eventually) Will start as a tree with duplication

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, serializer, assoc_list, each_serializer = nil) ⇒ ApiNode

Returns a new instance of ApiNode.



15
16
17
18
19
20
21
# File 'lib/simple_json_api/api_node.rb', line 15

def initialize(name, serializer, assoc_list, each_serializer = nil)
  @name = name
  @serializer = serializer
  @each_serializer = each_serializer
  @assoc_list = assoc_list
  @associations = [] # list of api_nodes
end

Instance Attribute Details

#assoc_listObject (readonly)

Returns the value of attribute assoc_list.



13
14
15
# File 'lib/simple_json_api/api_node.rb', line 13

def assoc_list
  @assoc_list
end

#associationsObject (readonly)

Returns the value of attribute associations.



12
13
14
# File 'lib/simple_json_api/api_node.rb', line 12

def associations
  @associations
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/simple_json_api/api_node.rb', line 10

def name
  @name
end

#serializerObject (readonly)

Returns the value of attribute serializer.



11
12
13
# File 'lib/simple_json_api/api_node.rb', line 11

def serializer
  @serializer
end

Instance Method Details

#<<(node) ⇒ Object



53
54
55
# File 'lib/simple_json_api/api_node.rb', line 53

def <<(node)
  add_assoc(node)
end

#add_assoc(node) ⇒ Object



57
58
59
# File 'lib/simple_json_api/api_node.rb', line 57

def add_assoc(node)
  @associations << node
end

#add_association(association) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/simple_json_api/api_node.rb', line 35

def add_association(association)
  return unless @assoc_list.key? association.plural_name
  resource = serializer.associated_object(association.name)
  Array(resource).each do |object|
    each_serializer = Serializer.for(object, association)
    serializer = SerializerFactory.create(
      object, each_serializer, self.serializer._builder
    )
    self <<
      ApiNode.new(
        association.plural_name,
        serializer,
        @assoc_list[association.plural_name],
        serializer._each_serializer
      ).load
  end
end

#loadObject



23
24
25
26
27
28
29
# File 'lib/simple_json_api/api_node.rb', line 23

def load
  return self unless serializer_actual._associations
  serializer_actual._associations.each do |association|
    add_association(association)
  end
  self
end

#serializer_actualObject



31
32
33
# File 'lib/simple_json_api/api_node.rb', line 31

def serializer_actual
  @serializer_actual ||= @each_serializer || self.serializer
end