Module: AbcJsonapi::Serializer

Extended by:
ActiveSupport::Concern
Defined in:
lib/abc_jsonapi/serializer.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#includesObject (readonly)

Returns the value of attribute includes.



14
15
16
# File 'lib/abc_jsonapi/serializer.rb', line 14

def includes
  @includes
end

#metaObject (readonly)

Returns the value of attribute meta.



14
15
16
# File 'lib/abc_jsonapi/serializer.rb', line 14

def meta
  @meta
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



14
15
16
# File 'lib/abc_jsonapi/serializer.rb', line 14

def relationships
  @relationships
end

#resourceObject (readonly)

Returns the value of attribute resource.



14
15
16
# File 'lib/abc_jsonapi/serializer.rb', line 14

def resource
  @resource
end

#resource_attributesObject (readonly)

Returns the value of attribute resource_attributes.



14
15
16
# File 'lib/abc_jsonapi/serializer.rb', line 14

def resource_attributes
  @resource_attributes
end

#resource_typeObject (readonly)

Returns the value of attribute resource_type.



14
15
16
# File 'lib/abc_jsonapi/serializer.rb', line 14

def resource_type
  @resource_type
end

#result_hashObject (readonly)

Returns the value of attribute result_hash.



14
15
16
# File 'lib/abc_jsonapi/serializer.rb', line 14

def result_hash
  @result_hash
end

#virtual_attributesObject (readonly)

Returns the value of attribute virtual_attributes.



14
15
16
# File 'lib/abc_jsonapi/serializer.rb', line 14

def virtual_attributes
  @virtual_attributes
end

Instance Method Details

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



23
24
25
26
27
28
29
30
31
32
# File 'lib/abc_jsonapi/serializer.rb', line 23

def initialize(resource, options = {})
  @resource = resource
  @result_hash = { data: nil }
  @resource_type = self.class.resource_type
  @resource_attributes = self.class.resource_attributes
  @relationships = self.class.relationships
  @virtual_attributes = self.class.virtual_attributes
  @includes = options[:include]
  @meta = options[:meta]
end

#serializable_hashObject



34
35
36
37
38
39
40
41
# File 'lib/abc_jsonapi/serializer.rb', line 34

def serializable_hash
  return nil if resource.nil?

  result_hash[:meta] = meta if meta.present?
  result_hash[:data] = data_hash
  result_hash[:included] = included_hash if @includes.present?
  result_hash
end

#serialized_jsonObject



43
44
45
# File 'lib/abc_jsonapi/serializer.rb', line 43

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