Class: SimpleJsonApi::JsonApiBuilder

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

Overview

The Builder to walk the hierarchy and contruct the JSON

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, serializer:, options: {}) ⇒ JsonApiBuilder

TODO: sort: nil



20
21
22
23
24
25
26
27
# File 'lib/simple_json_api/json_api_builder.rb', line 20

def initialize(model:, serializer:, options: {})
  @model = model
  handle_options(options, serializer)
  @serializer = get_serializer(serializer)
  @_included_resources = {}
  @result = {}
  @linked = {}
end

Instance Attribute Details

#_included_resourcesObject (readonly)

Returns the value of attribute _included_resources.



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

def _included_resources
  @_included_resources
end

#field_listObject (readonly)

Returns the value of attribute field_list.



14
15
16
# File 'lib/simple_json_api/json_api_builder.rb', line 14

def field_list
  @field_list
end

#includeObject (readonly)

Returns the value of attribute include.



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

def include
  @include
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#resultObject (readonly)

Returns the value of attribute result.



15
16
17
# File 'lib/simple_json_api/json_api_builder.rb', line 15

def result
  @result
end

#serializerObject (readonly)

Returns the value of attribute serializer.



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

def serializer
  @serializer
end

Instance Method Details

#add_linked_elem(elem, obj, association, base) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/simple_json_api/json_api_builder.rb', line 49

def add_linked_elem(elem, obj, association, base)
  resource_name = elem.to_s.pluralize
  obj = Array(obj) unless obj.respond_to?(:each)
  obj.each do |item|
    serialize_association(item, resource_name, association, base)
  end
end

#add_to_linked(assoc_base, item, serializer) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/simple_json_api/json_api_builder.rb', line 65

def add_to_linked(assoc_base, item, serializer)
  linked = @linked[serializer._root_name] ||= []
  return if linked.already_linked?(key: item.json_pk, id: item.json_id)
  linked << serializer.new(
    object: item, builder: self, base: assoc_base
  ).serialize
end

#as_json(options = nil) ⇒ Object



39
40
41
# File 'lib/simple_json_api/json_api_builder.rb', line 39

def as_json(options = nil)
  build.as_json(options)
end

#buildObject



43
44
45
46
47
# File 'lib/simple_json_api/json_api_builder.rb', line 43

def build
  result[@serializer._root_name] = @serializer.serialize
  @result[:linked] = @linked unless @linked.empty?
  @result
end

#handle_options(options, serializer) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/simple_json_api/json_api_builder.rb', line 29

def handle_options(options, serializer)
  @field_list = FieldList.new(
    fields: options.fetch(:fields, nil),
    root_serializer: serializer
  )
  @include = IncludeList.new(
    include: options.fetch(:include, nil)
  ).parse
end

#serialize_association(item, resource_name, association, base) ⇒ Object



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

def serialize_association(item, resource_name, association, base)
  # TODO: don't include middle unless explicitly requested.
  return unless include.include?(resource_name, base)
  assoc_base = [base, resource_name].compact.join('.')
  serializer = ResourceSerializer.for(item, association)
  add_to_linked(assoc_base, item, serializer)
end