Class: JSONAPI::ResourceSerializer
- Inherits:
-
Object
- Object
- JSONAPI::ResourceSerializer
- Defined in:
- lib/jsonapi/resource_serializer.rb
Instance Attribute Summary collapse
-
#url_generator ⇒ Object
readonly
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.
Instance Method Summary collapse
- #find_link(query_params) ⇒ Object
-
#initialize(primary_resource_klass, options = {}) ⇒ ResourceSerializer
constructor
A new instance of ResourceSerializer.
-
#serialize_to_hash(source) ⇒ Object
Converts a single resource, or an array of resources to a hash, conforming to the JSONAPI structure.
- #serialize_to_links_hash(source, requested_relationship) ⇒ Object
Constructor Details
#initialize(primary_resource_klass, options = {}) ⇒ ResourceSerializer
Returns a new instance of ResourceSerializer.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/jsonapi/resource_serializer.rb', line 17 def initialize(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) @url_generator = 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) end |
Instance Attribute Details
#url_generator ⇒ Object (readonly)
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
15 16 17 |
# File 'lib/jsonapi/resource_serializer.rb', line 15 def url_generator @url_generator end |
Instance Method Details
#find_link(query_params) ⇒ Object
73 74 75 |
# File 'lib/jsonapi/resource_serializer.rb', line 73 def find_link(query_params) url_generator.query_link(query_params) end |
#serialize_to_hash(source) ⇒ Object
Converts a single resource, or an array of resources to a hash, conforming to the JSONAPI structure
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jsonapi/resource_serializer.rb', line 31 def serialize_to_hash(source) is_resource_collection = source.respond_to?(:to_ary) @included_objects = {} @include_directives ||= JSONAPI::IncludeDirectives.new(@include) process_primary(source, @include_directives.include_directives) included_objects = [] primary_objects = [] @included_objects.each_value do |objects| objects.each_value do |object| if object[:primary] primary_objects.push(object[:object_hash]) else included_objects.push(object[:object_hash]) end end end primary_hash = { data: is_resource_collection ? primary_objects : primary_objects[0] } primary_hash[:included] = included_objects if included_objects.size > 0 primary_hash end |
#serialize_to_links_hash(source, requested_relationship) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jsonapi/resource_serializer.rb', line 57 def serialize_to_links_hash(source, requested_relationship) if requested_relationship.is_a?(JSONAPI::Relationship::ToOne) data = to_one_linkage(source, requested_relationship) else data = to_many_linkage(source, requested_relationship) end { links: { self: self_link(source, requested_relationship), related: (source, requested_relationship) }, data: data } end |