Class: ActiveModel::Serializer::Adapter::FragmentCache

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/serializer/adapter/fragment_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, serializer, options) ⇒ FragmentCache

Returns a new instance of FragmentCache.



4
5
6
7
8
# File 'lib/active_model/serializer/adapter/fragment_cache.rb', line 4

def initialize(adapter, serializer, options)
  @options    = options
  @adapter    = adapter
  @serializer = serializer
end

Instance Attribute Details

#serializerObject (readonly)

Returns the value of attribute serializer.



2
3
4
# File 'lib/active_model/serializer/adapter/fragment_cache.rb', line 2

def serializer
  @serializer
end

Instance Method Details

#fetchObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_model/serializer/adapter/fragment_cache.rb', line 10

def fetch
  klass = serializer.class
  # It will split the serializer into two, one that will be cached and other wont
  serializers = fragment_serializer(serializer.object.class.name, klass)

  # Instanciate both serializers
  cached_serializer     = serializers[:cached].constantize.new(serializer.object)
  non_cached_serializer = serializers[:non_cached].constantize.new(serializer.object)

  cached_adapter     = @adapter.class.new(cached_serializer, @options)
  non_cached_adapter = @adapter.class.new(non_cached_serializer, @options)

  # Get serializable hash from both
  cached_hash     = cached_adapter.serializable_hash
  non_cached_hash = non_cached_adapter.serializable_hash

  # Merge both results
  @adapter.fragment_cache(cached_hash, non_cached_hash)
end