Class: ActiveModelSerializers::FragmentCache

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model_serializers/fragment_cache.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

Returns a new instance of FragmentCache



5
6
7
8
9
# File 'lib/active_model_serializers/fragment_cache.rb', line 5

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



3
4
5
# File 'lib/active_model_serializers/fragment_cache.rb', line 3

def serializer
  @serializer
end

Instance Method Details

- (Object) fetch

  1. Create a CachedSerializer and NonCachedSerializer from the serializer class

  2. Serialize the above two with the given adapter

  3. Pass their serializations to the adapter ::fragment_cache



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

def fetch
  object = serializer.object

  # It will split the serializer into two, one that will be cached and one that will not
  serializers = fragment_serializer

  # Get serializable hash from both
  cached_hash     = serialize(object, serializers[:cached])
  non_cached_hash = serialize(object, serializers[:non_cached])

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