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



65
66
67
# File 'lib/simple_json_api/api_node.rb', line 65

def <<(node)
  add_assoc(node)
end

#add_assoc(node) ⇒ Object



69
70
71
# File 'lib/simple_json_api/api_node.rb', line 69

def add_assoc(node)
  @associations << node
end

#add_association(association) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/simple_json_api/api_node.rb', line 39

def add_association(association)
  return unless @assoc_list.key? association.plural_name
  resource = serializer.associated_object(association.name)
  Array(resource).each do |object|
    add_nodes(association, object)
  end
end

#add_node(association, serializer) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/simple_json_api/api_node.rb', line 55

def add_node(association, serializer)
  self <<
    ApiNode.new(
      association.plural_name,
      serializer,
      @assoc_list[association.plural_name],
      serializer._each_serializer
    ).load
end

#add_nodes(association, object) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/simple_json_api/api_node.rb', line 47

def add_nodes(association, object)
  each_serializer = Serializer.for(object, association)
  serializer = SerializerFactory.create(
    object, each_serializer, self.serializer._builder
  )
  add_node(association, serializer)
end

#collection?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/simple_json_api/api_node.rb', line 23

def collection?
  @serializer.is_a? ArraySerializer
end

#loadObject



27
28
29
30
31
32
33
# File 'lib/simple_json_api/api_node.rb', line 27

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

#serializer_actualObject



35
36
37
# File 'lib/simple_json_api/api_node.rb', line 35

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