Module: Cuprum::Rails::Responders::Serialization

Included in:
JsonResponder
Defined in:
lib/cuprum/rails/responders/serialization.rb

Overview

Implements serializing a result value into response data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#serializersHash<Class, Object> (readonly)

Returns The serializers for converting result values into serialized data.

Returns:

  • (Hash<Class, Object>)

    The serializers for converting result values into serialized data.



21
22
23
# File 'lib/cuprum/rails/responders/serialization.rb', line 21

def serializers
  @serializers
end

Instance Method Details

#initialize(serializers:, **options) ⇒ Object

Parameters:

  • serializers (Hash<Class, Object>)

    The serializers for converting result values into serialized data.

  • options (Hash)

    Additional parameters for the responder.



13
14
15
16
17
# File 'lib/cuprum/rails/responders/serialization.rb', line 13

def initialize(serializers:, **options)
  super(**options)

  @serializers = serializers
end

#serialize(object) ⇒ Object

Converts a result value into a serialized data structure.

Parameters:

  • object (Object)

    The object to serialize.

Returns:

  • (Object)

    the serialized data.



28
29
30
31
32
33
34
35
36
# File 'lib/cuprum/rails/responders/serialization.rb', line 28

def serialize(object)
  context = Cuprum::Rails::Serializers::Context.new(
    serializers: serializers
  )

  Cuprum::Rails::Serializers::BaseSerializer
    .instance
    .call(object, context: context)
end