Module: AbcJsonapi::Serializer
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/abc_jsonapi/serializer.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#includes ⇒ Object
readonly
Returns the value of attribute includes.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#relationships ⇒ Object
readonly
Returns the value of attribute relationships.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#resource_attributes ⇒ Object
readonly
Returns the value of attribute resource_attributes.
-
#resource_type ⇒ Object
readonly
Returns the value of attribute resource_type.
-
#result_hash ⇒ Object
readonly
Returns the value of attribute result_hash.
-
#virtual_attributes ⇒ Object
readonly
Returns the value of attribute virtual_attributes.
Instance Method Summary collapse
Instance Attribute Details
#includes ⇒ Object (readonly)
Returns the value of attribute includes.
14 15 16 |
# File 'lib/abc_jsonapi/serializer.rb', line 14 def includes @includes end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
14 15 16 |
# File 'lib/abc_jsonapi/serializer.rb', line 14 def end |
#relationships ⇒ Object (readonly)
Returns the value of attribute relationships.
14 15 16 |
# File 'lib/abc_jsonapi/serializer.rb', line 14 def relationships @relationships end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
14 15 16 |
# File 'lib/abc_jsonapi/serializer.rb', line 14 def resource @resource end |
#resource_attributes ⇒ Object (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_type ⇒ Object (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_hash ⇒ Object (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_attributes ⇒ Object (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
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/abc_jsonapi/serializer.rb', line 17 def initialize(resource, = {}) @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 = [:include] = [:meta] end |
#serializable_hash ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/abc_jsonapi/serializer.rb', line 28 def serializable_hash return nil if resource.nil? result_hash[:meta] = if .present? result_hash[:data] = data_hash result_hash[:included] = included_hash if @includes.present? result_hash end |
#serialized_json ⇒ Object
37 38 39 |
# File 'lib/abc_jsonapi/serializer.rb', line 37 def serialized_json ActiveSupport::JSON.encode(serializable_hash) end |