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



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

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



63
64
65
66
67
# File 'lib/simple_json_api/json_api_builder.rb', line 63

def add_to_linked(assoc_base, item, serializer)
  linked = @linked[serializer._root_name] ||= []
  return if linked.already_linked?(item.json_pk, item.json_id)
  linked << serializer.new(item, self, nil, assoc_base).serialize
end

#as_json(options = nil) ⇒ Object



37
38
39
# File 'lib/simple_json_api/json_api_builder.rb', line 37

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

#buildObject



41
42
43
44
45
# File 'lib/simple_json_api/json_api_builder.rb', line 41

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
# File 'lib/simple_json_api/json_api_builder.rb', line 29

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

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



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

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