Module: FastJsonapi::ObjectSerializer

Extended by:
ActiveSupport::Concern
Includes:
SerializationCore
Defined in:
lib/fast_jsonapi/object_serializer.rb

Constant Summary collapse

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



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

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

  @resource = resource
end

#serializable_hashObject Also known as: to_hash



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

def serializable_hash
  if self.class.is_collection?(@resource, @is_collection)
    return hash_for_collection
  end

  hash_for_one_record
end