Class: Transmutation::Serialization::Lookup Private

Inherits:
Object
  • Object
show all
Defined in:
lib/transmutation/serialization/lookup.rb,
lib/transmutation/serialization/lookup/serializer_not_found.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: SerializerNotFound

Instance Method Summary collapse

Constructor Details

#initialize(caller, namespace: nil) ⇒ Lookup

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Lookup.



7
8
9
10
# File 'lib/transmutation/serialization/lookup.rb', line 7

def initialize(caller, namespace: nil)
  @caller = caller
  @namespace = namespace
end

Instance Method Details

#serializer_for(object, serializer: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This never bubbles up the object’s namespace, only the caller’s namespace.

Bubbles up the namespace until we find a matching serializer.

Examples:

namespace: Api::V1::Admin::Detailed
serializer: Chat::User

# This method will attempt to find a serializer defined in the following order:

# - Api::V1::Admin::Detailed::Chat::UserSerializer
# - Api::V1::Admin::Chat::UserSerializer
# - Api::V1::Chat::UserSerializer
# - Api::Chat::UserSerializer
# - Chat::UserSerializer

See Also:



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/transmutation/serialization/lookup.rb', line 28

def serializer_for(object, serializer: nil)
  serializer_name = serializer_name_for(object, serializer:)

  return constantize_serializer!(Object, serializer_name, object:) if serializer_name.start_with?("::")

  potential_namespaces.each do |potential_namespace|
    return potential_namespace.const_get(serializer_name) if potential_namespace.const_defined?(serializer_name)
  end

  Transmutation::ObjectSerializer
end

#serializer_name_for(object, serializer: nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the highest specificity serializer name for the given object.

Parameters:

  • object (Object)

    The object to find the serializer name for.

Returns:

  • (String)

    The serializer name.



45
46
47
# File 'lib/transmutation/serialization/lookup.rb', line 45

def serializer_name_for(object, serializer: nil)
  "#{serializer&.delete_suffix("Serializer") || object.class.name}Serializer"
end