Class: Cuprum::Rails::Serializers::BaseSerializer
- Inherits:
-
Object
- Object
- Cuprum::Rails::Serializers::BaseSerializer
- Defined in:
- lib/cuprum/rails/serializers/base_serializer.rb
Overview
Converts objects or data structures based on configured serializers.
Direct Known Subclasses
Json::ActiveRecordSerializer, Json::ArraySerializer, Json::ErrorSerializer, Json::HashSerializer, Json::IdentitySerializer, Json::PropertiesSerializer
Defined Under Namespace
Classes: RecursiveSerializerError, UndefinedSerializerError
Class Method Summary collapse
-
.instance ⇒ Cuprum::Rails::Serializers::Serializer
A cached instance of the serializer.
Instance Method Summary collapse
-
#call(object, context:) ⇒ Object
Converts the object to a serialized representation.
Class Method Details
.instance ⇒ Cuprum::Rails::Serializers::Serializer
Returns a cached instance of the serializer.
16 17 18 |
# File 'lib/cuprum/rails/serializers/base_serializer.rb', line 16 def self.instance @instance ||= new end |
Instance Method Details
#call(object, context:) ⇒ Object
Converts the object to a serialized representation.
First, #call finds the best serializer from the :serializers Hash. This is done by walking up the object class’s ancestors to find the closest ancestor which is a key in the :serializers Hash. The corresponding value is then called with the object.
38 39 40 41 42 |
# File 'lib/cuprum/rails/serializers/base_serializer.rb', line 38 def call(object, context:) handle_recursion!(object, context: context) context.serialize(object) end |