Class: SimpleJsonApi::JsonApiBuilder
- Inherits:
-
Object
- Object
- SimpleJsonApi::JsonApiBuilder
- 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
-
#_included_resources ⇒ Object
readonly
Returns the value of attribute _included_resources.
-
#field_list ⇒ Object
readonly
Returns the value of attribute field_list.
-
#include ⇒ Object
readonly
Returns the value of attribute include.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
Instance Method Summary collapse
- #add_linked_elem(elem, obj, association, base) ⇒ Object
- #add_to_linked(assoc_base, item, serializer) ⇒ Object
- #as_json(options = nil) ⇒ Object
- #build ⇒ Object
- #handle_options(options, serializer) ⇒ Object
-
#initialize(model, serializer, options = {}) ⇒ JsonApiBuilder
constructor
TODO: sort: nil.
- #serialize_association(item, resource_name, association, base) ⇒ Object
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, = {}) @model = model (, serializer) @serializer = get_serializer(serializer) @_included_resources = {} @result = {} @linked = {} end |
Instance Attribute Details
#_included_resources ⇒ Object (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_list ⇒ Object (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 |
#include ⇒ Object (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 |
#model ⇒ Object (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 |
#result ⇒ Object (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 |
#serializer ⇒ Object (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( = nil) build.as_json() end |
#build ⇒ Object
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 (, serializer) @field_list = FieldList.new( .fetch(:fields, nil), serializer ) @include = IncludeList.new(.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 |