Class: FastSerializer::SerializationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_serializer/serialization_context.rb

Overview

This class provides a context for creating serializers that allows duplicate serializers to be re-used within the context. This then short circuits the serialization process on the duplicates.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSerializationContext

Returns a new instance of SerializationContext.



35
36
37
38
# File 'lib/fast_serializer/serialization_context.rb', line 35

def initialize
  @cache = nil
  @references = nil
end

Class Method Details

.currentFastSerializer::SerializationContext?

Return the current context or nil if none is in use.



30
31
32
# File 'lib/fast_serializer/serialization_context.rb', line 30

def current
  Thread.current[:fast_serializer_context]
end

.useObject

Use a context or create one for use within a block. Any serializers based on the same object with the same options within the block will be re-used instead of creating duplicates.

Returns:

  • (Object)

    The return value of the block.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fast_serializer/serialization_context.rb', line 14

def use
  if Thread.current[:fast_serializer_context]
    yield
  else
    begin
      Thread.current[:fast_serializer_context] = new
      yield
    ensure
      Thread.current[:fast_serializer_context] = nil
    end
  end
end

Instance Method Details

#load(serializer_class, object, options = nil) ⇒ FastSerializer::Serializer

Returns a serializer from the context cache if one has already been created. Otherwise creates the serializer and adds it to the cache.

Parameters:

  • serializer_class (Class)

    The serializer class to create.

  • object (Object)

    The object to serialize.

  • options (Hash) (defaults to: nil)

    The options to pass to the serializer.

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fast_serializer/serialization_context.rb', line 48

def load(serializer_class, object, options = nil)
  key = [serializer_class, object, options]
  serializer = nil
  if @cache
    serializer = @cache[key]
  end

  unless serializer
    serializer = serializer_class.allocate
    serializer.send(:initialize, object, options)
    @cache = {}
    @cache[key] = serializer
  end

  serializer
end

#with_reference(object) { ... } ⇒ Object

Maintain reference stack to avoid circular references.

Parameters:

  • object (Object)

    The object to check for circular references.

Yields:

  • The block to execute.

Returns:

  • The return value of the block.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fast_serializer/serialization_context.rb', line 70

def with_reference(object)
  if @references
    raise CircularReferenceError.new(object) if @references.include?(object)
  else
    @references = []
  end

  begin
    @references.push(object)
    yield
  ensure
    @references.pop
  end
end