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 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
# 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)
  @id_formatter       = ValueFormatter.value_formatter_for(:id)
  @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, {})

  # 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

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


86
87
88
# File 'lib/jsonapi/resource_serializer.rb', line 86

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

#format_value(value, format) ⇒ Object


90
91
92
# File 'lib/jsonapi/resource_serializer.rb', line 90

def format_value(value, format)
  @value_formatter_type_cache.get(format).format(value)
end

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

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


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jsonapi/resource_serializer.rb', line 38

def serialize_to_hash(source)
  @top_level_sources = Set.new([source].flatten.compact.map {|s| top_level_source_key(s) })

  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

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jsonapi/resource_serializer.rb', line 66

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