Method: JSONAPI::ResourceSerializer#initialize
- Defined in:
- lib/jsonapi/resource_serializer.rb
#initialize(primary_resource_klass, options = {}) ⇒ ResourceSerializer
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 instance to override the default configuration serializer_options: additional options that will be passed to resource meta and links lambdas
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/jsonapi/resource_serializer.rb', line 18 def initialize(primary_resource_klass, = {}) @primary_resource_klass = primary_resource_klass @primary_class_name = primary_resource_klass._type @fields = .fetch(:fields, {}) @include = .fetch(:include, []) @include_directives = [:include_directives] @key_formatter = .fetch(:key_formatter, JSONAPI.configuration.key_formatter) @id_formatter = ValueFormatter.value_formatter_for(:id) @link_builder = generate_link_builder(primary_resource_klass, ) @always_include_to_one_linkage_data = .fetch(:always_include_to_one_linkage_data, JSONAPI.configuration.always_include_to_one_linkage_data) @always_include_to_many_linkage_data = .fetch(:always_include_to_many_linkage_data, JSONAPI.configuration.always_include_to_many_linkage_data) @serialization_options = .fetch(:serialization_options, {}) # Warning: This makes ResourceSerializer non-thread-safe. That's not a problem with the # request-specific way it's currently used, though. @value_formatter_type_cache = NaiveCache.new{|arg| ValueFormatter.value_formatter_for(arg) } end |