Module: FastJsonapi::ObjectSerializer

Extended by:
ActiveSupport::Concern
Includes:
SerializationCore
Defined in:
lib/fast_jsonapi/object_serializer.rb,
lib/fast_jsonapi/instrumentation/serialized_json.rb,
lib/fast_jsonapi/instrumentation/serializable_hash.rb

Constant Summary collapse

SERIALIZABLE_HASH_NOTIFICATION =
'render.fast_jsonapi.serializable_hash'
SERIALIZED_JSON_NOTIFICATION =
'render.fast_jsonapi.serialized_json'
TRANSFORMS_MAPPING =
{
  camel: :camelize,
  camel_lower: [:camelize, :lower],
  dash: :dasherize,
  underscore: :underscore
}.freeze

Instance Method Summary collapse

Instance Method Details

#hash_for_collectionObject



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

def hash_for_collection
  serializable_hash = {}

  data = []
  included = []
  fieldset = @fieldsets[self.class.record_type.to_sym]
  @resource.each do |record|
    data << self.class.record_hash(record, fieldset, @includes, @params)
    included.concat self.class.get_included_records(record, @includes, @known_included_objects, @fieldsets, @params) if @includes.present?
  end

  serializable_hash[:data] = data
  serializable_hash[:included] = included if @includes.present?
  serializable_hash[:meta] = @meta if @meta.present?
  serializable_hash[:links] = @links if @links.present?
  serializable_hash
end

#hash_for_one_recordObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fast_jsonapi/object_serializer.rb', line 45

def hash_for_one_record
  serializable_hash = { data: nil }
  serializable_hash[:meta] = @meta if @meta.present?
  serializable_hash[:links] = @links if @links.present?

  return serializable_hash unless @resource

  serializable_hash[:data] = self.class.record_hash(@resource, @fieldsets[self.class.record_type.to_sym], @includes, @params)
  serializable_hash[:included] = self.class.get_included_records(@resource, @includes, @known_included_objects, @fieldsets, @params) if @includes.present?
  serializable_hash
end

#initialize(resource, options = {}) ⇒ Object



32
33
34
35
36
# File 'lib/fast_jsonapi/object_serializer.rb', line 32

def initialize(resource, options = {})
  process_options(options)

  @resource = resource
end

#serializable_hashObject



38
39
40
41
42
# File 'lib/fast_jsonapi/object_serializer.rb', line 38

def serializable_hash
  return hash_for_collection if is_collection?(@resource, @is_collection)

  hash_for_one_record
end

#serializable_hash_without_instrumentationObject



5
6
7
8
9
# File 'lib/fast_jsonapi/instrumentation/serializable_hash.rb', line 5

def serializable_hash
  return hash_for_collection if is_collection?(@resource, @is_collection)

  hash_for_one_record
end

#serialized_jsonObject



7
8
9
10
11
# File 'lib/fast_jsonapi/instrumentation/serialized_json.rb', line 7

def serialized_json
  ActiveSupport::Notifications.instrument(SERIALIZED_JSON_NOTIFICATION, { name: self.class.name }) do
    serialized_json_without_instrumentation
  end
end

#serialized_json_without_instrumentationObject



5
# File 'lib/fast_jsonapi/instrumentation/serialized_json.rb', line 5

alias serialized_json_without_instrumentation serialized_json

#to_hashObject



43
44
45
46
47
# File 'lib/fast_jsonapi/object_serializer.rb', line 43

def serializable_hash
  return hash_for_collection if is_collection?(@resource, @is_collection)

  hash_for_one_record
end