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.



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

def includes
  @includes
end

#metaObject (readonly)

Returns the value of attribute meta.



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

def meta
  @meta
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



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

def relationships
  @relationships
end

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

#resource_attributesObject (readonly)

Returns the value of attribute resource_attributes.



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

def resource_attributes
  @resource_attributes
end

#resource_typeObject (readonly)

Returns the value of attribute resource_type.



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

def resource_type
  @resource_type
end

#result_hashObject (readonly)

Returns the value of attribute result_hash.



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

def result_hash
  @result_hash
end

#virtual_attributesObject (readonly)

Returns the value of attribute virtual_attributes.



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

def virtual_attributes
  @virtual_attributes
end

Instance Method Details

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



16
17
18
19
20
21
22
23
24
25
# File 'lib/abc_jsonapi/serializer.rb', line 16

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



27
28
29
30
31
32
33
34
# File 'lib/abc_jsonapi/serializer.rb', line 27

def serializable_hash
  return result_hash if resource.blank?

  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



36
37
38
# File 'lib/abc_jsonapi/serializer.rb', line 36

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