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'

Instance Method Summary collapse

Instance Method Details

#hash_for_collectionObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fast_jsonapi/object_serializer.rb', line 51

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, @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



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fast_jsonapi/object_serializer.rb', line 39

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], @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



26
27
28
29
30
# File 'lib/fast_jsonapi/object_serializer.rb', line 26

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

  @resource = resource
end

#serializable_hashObject



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

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

  hash_for_one_record
end

#serializable_hash_without_instrumentationObject



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

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

  hash_for_one_record
end

#serialized_jsonObject



69
70
71
# File 'lib/fast_jsonapi/object_serializer.rb', line 69

def serialized_json
  ActiveSupport::JSON.encode(serializable_hash)
end

#serialized_json_without_instrumentationObject



6
7
8
# File 'lib/fast_jsonapi/instrumentation/serialized_json.rb', line 6

def serialized_json
  ActiveSupport::JSON.encode(serializable_hash)
end

#to_hashObject



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

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

  hash_for_one_record
end