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.
13 14 15 |
# File 'lib/abc_jsonapi/serializer.rb', line 13 def includes @includes end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
13 14 15 |
# File 'lib/abc_jsonapi/serializer.rb', line 13 def @meta end |
#relationships ⇒ Object (readonly)
Returns the value of attribute relationships.
13 14 15 |
# File 'lib/abc_jsonapi/serializer.rb', line 13 def relationships @relationships end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
13 14 15 |
# File 'lib/abc_jsonapi/serializer.rb', line 13 def resource @resource end |
#resource_attributes ⇒ Object (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_type ⇒ Object (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_hash ⇒ Object (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_attributes ⇒ Object (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, = {}) @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 = [:meta] end |
#serializable_hash ⇒ Object
27 28 29 30 31 32 |
# File 'lib/abc_jsonapi/serializer.rb', line 27 def serializable_hash result_hash[:meta] = if .present? result_hash[:data] = data_hash result_hash[:included] = included_hash if @includes.present? result_hash end |
#serialized_json ⇒ Object
34 35 36 |
# File 'lib/abc_jsonapi/serializer.rb', line 34 def serialized_json ActiveSupport::JSON.encode(serializable_hash) end |