Class: JsonApiServer::ResourcesSerializer
- Inherits:
-
ResourceSerializer
- Object
- BaseSerializer
- ResourceSerializer
- JsonApiServer::ResourcesSerializer
- Defined in:
- lib/json_api_server/resources_serializer.rb
Overview
Description
Serializer for a collection/array of resources. Inherits from JsonApiServer::ResourceSerializer.
Example
Given a resource serializer for Topic:
class TopicSerializer < JsonApiServer::ResourceSerializer
resource_type 'topics'
def links
{ self: File.join(base_url, "/topics/#{@object.id}") }
end
def data
{
type: self.class.type,
id: @object.id,
attributes: attributes
}
end
protected
def attributes
attributes_builder
.add('book', @object.book)
.add('author', @object.)
.add('quote', @object.quote)
.add('character', @object.character)
.add('location', @object.location)
.add('published', @object.published)
.add('created_at', @object.created_at.try(:iso8601, 0))
.add('updated_at', @object.updated_at.try(:iso8601, 0))
.attributes
end
end
Create a Topics serializer like so:
class TopicsSerializer < JsonApiServer::ResourcesSerializer
serializer TopicSerializer
end
Create an instance from builder:
builder = JsonApiServer::Builder.new(request, Topic.all)
.add_pagination()
.add_filter()
.add_sort()
.add_include()
.add_fields
# populates links with pagination info, merges data from each
# Topic serializer instance.
serializer = TopicsSerializer.from_builder(builder)
Class Attribute Summary collapse
-
.objects_serializer ⇒ Object
readonly
Returns the value of attribute objects_serializer.
Instance Attribute Summary collapse
-
#filter ⇒ Object
readonly
Instance of JsonApiServer::Filter or nil (default).
-
#paginator ⇒ Object
readonly
Instance of JsonApiServer::Paginator or nil (default).
Attributes inherited from ResourceSerializer
Attributes inherited from BaseSerializer
Class Method Summary collapse
-
.serializer(klass) ⇒ Object
A serializer class.
Instance Method Summary collapse
-
#data ⇒ Object
Subclasses override for customized behaviour.
-
#included ⇒ Object
Subclasses override for customized behaviour.
-
#initialize(objects, **options) ⇒ ResourcesSerializer
constructor
-
objects
- An array of objects.
-
- #links ⇒ Object
-
#relationship_data ⇒ Object
Subclasses override for customized behaviour.
Methods inherited from ResourceSerializer
from_builder, resource_type, type
Methods inherited from BaseSerializer
Methods included from ApiVersion
Methods included from Serializer
#as_json, #serializer_options, #to_json
Constructor Details
#initialize(objects, **options) ⇒ ResourcesSerializer
-
objects
- An array of objects. If #serializer is specified, the objects will be converted to this class. -
options
- Hash:-
filter
- Instance of JsonApiServer::Filter or nil. Sets #filter. -
:paginator
- Instance of JsonApiServer::Fields or nil. Sets #paginator. -
:as_json_options
- See options at JsonApiServer::BaseSerializer#as_json_options.
-
89 90 91 92 93 94 95 |
# File 'lib/json_api_server/resources_serializer.rb', line 89 def initialize(objects, **) super(nil, ) remove_instance_variable(:@object) @paginator = [:paginator] @filter = [:filter] @objects = initalize_objects(objects) end |
Class Attribute Details
.objects_serializer ⇒ Object (readonly)
Returns the value of attribute objects_serializer.
74 75 76 |
# File 'lib/json_api_server/resources_serializer.rb', line 74 def objects_serializer @objects_serializer end |
Instance Attribute Details
#filter ⇒ Object (readonly)
Instance of JsonApiServer::Filter or nil (default). Based on filter params. Extracted via JsonApiServer::Filter and available through JsonApiServer::Builder#filter. Set in initializer options.
71 72 73 |
# File 'lib/json_api_server/resources_serializer.rb', line 71 def filter @filter end |
#paginator ⇒ Object (readonly)
Instance of JsonApiServer::Paginator or nil (default). Based on pagination params. Extracted via JsonApiServer::Pagination and available through JsonApiServer::Builder#paginator. Set in initializer options.
66 67 68 |
# File 'lib/json_api_server/resources_serializer.rb', line 66 def paginator @paginator end |
Class Method Details
.serializer(klass) ⇒ Object
A serializer class. If set,‘objects’ will be converted to instances of this serializer.
78 79 80 |
# File 'lib/json_api_server/resources_serializer.rb', line 78 def serializer(klass) @objects_serializer = klass end |
Instance Method Details
#data ⇒ Object
Subclasses override for customized behaviour.
102 103 104 105 |
# File 'lib/json_api_server/resources_serializer.rb', line 102 def data data = @objects.try(:map) { |o| o.try(:data) } data.try(:compact!) || data end |
#included ⇒ Object
Subclasses override for customized behaviour.
114 115 116 117 118 |
# File 'lib/json_api_server/resources_serializer.rb', line 114 def included included = @objects.try(:map) { |o| o.try(:included) } included.try(:flatten!) included.try(:compact!) || included end |
#links ⇒ Object
97 98 99 |
# File 'lib/json_api_server/resources_serializer.rb', line 97 def links @paginator.try(:as_json) || {} end |
#relationship_data ⇒ Object
Subclasses override for customized behaviour.
108 109 110 111 |
# File 'lib/json_api_server/resources_serializer.rb', line 108 def relationship_data data = @objects.try(:map) { |o| o.try(:relationship_data) } data.try(:compact!) || data end |