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



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

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



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

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



24
25
26
27
28
# File 'lib/fast_jsonapi/object_serializer.rb', line 24

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

  @resource = resource
end

#serializable_hashObject



30
31
32
33
34
# File 'lib/fast_jsonapi/object_serializer.rb', line 30

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



67
68
69
# File 'lib/fast_jsonapi/object_serializer.rb', line 67

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



35
36
37
38
39
# File 'lib/fast_jsonapi/object_serializer.rb', line 35

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

  hash_for_one_record
end