Module: Transmutation::Serialization

Included in:
Serializer
Defined in:
lib/transmutation/serialization.rb,
lib/transmutation/serialization/lookup.rb,
lib/transmutation/serialization/rendering.rb,
lib/transmutation/serialization/lookup/serializer_not_found.rb

Defined Under Namespace

Modules: Rendering Classes: Lookup

Instance Method Summary collapse

Instance Method Details

#lookup_serializer(object, namespace: nil, serializer: nil) ⇒ Class<Transmutation::Serializer>

Lookup the serializer for the given object.

This calls Transmutation::Serialization::Lookup#serializer_for to find the serializer for the given object.

Parameters:

  • object (Object)

    The object to lookup the serializer for.

  • namespace (String, Symbol, Module) (defaults to: nil)

    The namespace to lookup the serializer in.

  • serializer (String, Symbol, Class) (defaults to: nil)

    The serializer to use.

Returns:



35
36
37
# File 'lib/transmutation/serialization.rb', line 35

def lookup_serializer(object, namespace: nil, serializer: nil)
  Lookup.new(self, namespace: namespace).serializer_for(object, serializer: serializer)
end

#serialize(object, namespace: nil, serializer: nil, depth: 0, max_depth: 1) ⇒ Transmutation::Serializer

Serialize a given object with the looked up serializer.

Parameters:

  • object (Object)

    The object to serialize.

  • namespace (String, Symbol, Module) (defaults to: nil)

    The namespace to lookup the serializer in.

  • serializer (String, Symbol, Class) (defaults to: nil)

    The serializer to use.

  • max_depth (Integer) (defaults to: 1)

    The maximum depth of nested associations to serialize.

Returns:



14
15
16
17
18
19
20
21
22
23
# File 'lib/transmutation/serialization.rb', line 14

def serialize(object, namespace: nil, serializer: nil, depth: 0, max_depth: 1)
  if object.respond_to?(:map)
    return object.map do |item|
      serialize(item, namespace: namespace, serializer: serializer, depth: depth, max_depth: max_depth)
    end
  end

  lookup_serializer(object, namespace: namespace, serializer: serializer)
    .new(object, depth: depth, max_depth: max_depth)
end