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

- (FragmentCache) initialize(adapter, serializer, options)

Returns a new instance of FragmentCache



7
8
9
10
11
# File 'lib/active_model/serializer/adapter/fragment_cache.rb', line 7

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

Instance Attribute Details

- (Object) serializer (readonly)

Returns the value of attribute serializer



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

def serializer
  @serializer
end

Instance Method Details

- (Object) fetch



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

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, instance_options)
  non_cached_adapter = adapter.class.new(non_cached_serializer, instance_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