Class: JSONAPI::ResourceSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/resource_serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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 class 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
# File 'lib/jsonapi/resource_serializer.rb', line 18

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]
  @key_formatter      = options.fetch(:key_formatter, JSONAPI.configuration.key_formatter)
  @link_builder       = 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

Instance Attribute Details

#key_formatterObject (readonly)

Returns the value of attribute key_formatter.



4
5
6
# File 'lib/jsonapi/resource_serializer.rb', line 4

def key_formatter
  @key_formatter
end

Returns the value of attribute link_builder.



4
5
6
# File 'lib/jsonapi/resource_serializer.rb', line 4

def link_builder
  @link_builder
end

#primary_class_nameObject (readonly)

Returns the value of attribute primary_class_name.



4
5
6
# File 'lib/jsonapi/resource_serializer.rb', line 4

def primary_class_name
  @primary_class_name
end

#serialization_optionsObject (readonly)

Returns the value of attribute serialization_options.



4
5
6
# File 'lib/jsonapi/resource_serializer.rb', line 4

def serialization_options
  @serialization_options
end

Instance Method Details

#format_key(key) ⇒ Object



79
80
81
# File 'lib/jsonapi/resource_serializer.rb', line 79

def format_key(key)
  @key_formatter.format(key)
end

#format_value(value, format) ⇒ Object



83
84
85
86
# File 'lib/jsonapi/resource_serializer.rb', line 83

def format_value(value, format)
  value_formatter = JSONAPI::ValueFormatter.value_formatter_for(format)
  value_formatter.format(value)
end


75
76
77
# File 'lib/jsonapi/resource_serializer.rb', line 75

def query_link(query_params)
  link_builder.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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jsonapi/resource_serializer.rb', line 33

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


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jsonapi/resource_serializer.rb', line 59

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: related_link(source, requested_relationship)
    },
    data: data
  }
end