lib/jsonapi/resource_serializer.rb in jsonapi-resources-0.6.1 vs lib/jsonapi/resource_serializer.rb in jsonapi-resources-0.6.2
- old
+ new
@@ -1,21 +1,22 @@
module JSONAPI
class ResourceSerializer
+ attr_reader :url_generator, :key_formatter, :serialization_options, :primary_class_name
+
+ # initialize
# Options can include
# include:
# Purpose: determines which objects will be side loaded with the source objects in a linked section
# Example: ['comments','author','comments.tags','author.posts']
# fields:
# Purpose: determines which fields are serialized for a resource type. This encompasses both attributes and
# relationship ids in the links section for a resource. Fields are global for a resource type.
# Example: { people: [:id, :email, :comments], posts: [:id, :title, :author], comments: [:id, :body, :post]}
# key_formatter: KeyFormatter class to override the default configuration
- # base_url: a string to prepend to generated resource links
+ # serializer_options: additional options that will be passed to resource meta and links lambdas
- attr_reader :url_generator
-
def initialize(primary_resource_klass, options = {})
@primary_class_name = primary_resource_klass._type
@fields = options.fetch(:fields, {})
@include = options.fetch(:include, [])
@include_directives = options[:include_directives]
@@ -23,10 +24,11 @@
@url_generator = generate_link_builder(primary_resource_klass, options)
@always_include_to_one_linkage_data = options.fetch(:always_include_to_one_linkage_data,
JSONAPI.configuration.always_include_to_one_linkage_data)
@always_include_to_many_linkage_data = options.fetch(:always_include_to_many_linkage_data,
JSONAPI.configuration.always_include_to_many_linkage_data)
+ @serialization_options = options.fetch(:serialization_options, {})
end
# Converts a single resource, or an array of resources to a hash, conforming to the JSONAPI structure
def serialize_to_hash(source)
is_resource_collection = source.respond_to?(:to_ary)
@@ -72,10 +74,19 @@
def find_link(query_params)
url_generator.query_link(query_params)
end
+ def format_key(key)
+ @key_formatter.format(key)
+ end
+
+ def format_value(value, format)
+ value_formatter = JSONAPI::ValueFormatter.value_formatter_for(format)
+ value_formatter.format(value)
+ end
+
private
# Process the primary source object(s). This will then serialize associated object recursively based on the
# requested includes. Fields are controlled fields option for each resource type, such
# as fields: { people: [:id, :email, :comments], posts: [:id, :title, :author], comments: [:id, :body, :post]}
@@ -117,10 +128,14 @@
obj_hash['attributes'] = attributes unless attributes.empty?
relationships = relationship_data(source, include_directives)
obj_hash['relationships'] = relationships unless relationships.nil? || relationships.empty?
+ meta = source.meta(custom_generation_options)
+ if meta.is_a?(Hash) && !meta.empty?
+ obj_hash['meta'] = meta
+ end
obj_hash
end
def requested_fields(klass)
return if @fields.nil? || @fields.empty?
@@ -142,10 +157,17 @@
hash[format_key(name)] = format_value(source.public_send(name), format)
end
end
end
+ def custom_generation_options
+ {
+ serializer: self,
+ serialization_options: @serialization_options
+ }
+ end
+
def relationship_data(source, include_directives)
relationships = source.class._relationships
requested = requested_fields(source.class)
fields = relationships.keys
fields = requested & fields unless requested.nil?
@@ -309,18 +331,9 @@
@included_objects[type][id][:object_hash].merge!(object_hash)
set_primary(type, id) if primary
else
@included_objects[type].store(id, primary: primary, object_hash: object_hash)
end
- end
-
- def format_key(key)
- @key_formatter.format(key)
- end
-
- def format_value(value, format)
- value_formatter = JSONAPI::ValueFormatter.value_formatter_for(format)
- value_formatter.format(value)
end
def generate_link_builder(primary_resource_klass, options)
LinkBuilder.new(
base_url: options.fetch(:base_url, ''),