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
Class Method Summary collapse
- .cache ⇒ Object private
Instance Method Summary collapse
-
#lookup_serializer(object, namespace: nil, serializer: nil) ⇒ Class<Transmutation::Serializer>
Lookup the serializer for the given object.
-
#namespace ⇒ String
Returns the namespace of this class.
-
#serialize(object, namespace: nil, serializer: nil, depth: 0, max_depth: Transmutation.max_depth) ⇒ Transmutation::Serializer
Serialize a given object with the looked up serializer.
Class Method Details
.cache ⇒ 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.
39 40 41 |
# File 'lib/transmutation/serialization.rb', line 39 def self.cache @cache ||= {} end |
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.
This also caches the result for future lookups.
33 34 35 36 |
# File 'lib/transmutation/serialization.rb', line 33 def lookup_serializer(object, namespace: nil, serializer: nil) Serialization.cache[[self.namespace, object.class, namespace, serializer]] ||= Lookup.new(self, namespace:).serializer_for(object, serializer:) end |
#namespace ⇒ String
Returns the namespace of this class.
49 50 51 |
# File 'lib/transmutation/serialization.rb', line 49 def namespace @namespace ||= self.class.name.to_s[0, self.class.name.rindex("::") || 0] end |
#serialize(object, namespace: nil, serializer: nil, depth: 0, max_depth: Transmutation.max_depth) ⇒ Transmutation::Serializer
Serialize a given object with the looked up serializer.
13 14 15 16 17 18 19 |
# File 'lib/transmutation/serialization.rb', line 13 def serialize(object, namespace: nil, serializer: nil, depth: 0, max_depth: Transmutation.max_depth) if object.respond_to?(:map) && !object.respond_to?(:to_hash) return object.map { |item| serialize(item, namespace:, serializer:, depth:, max_depth:) } end lookup_serializer(object, namespace:, serializer:).new(object, depth:, max_depth:) end |