Class: EasySerializer::Cacher::Collection

Inherits:
Template
  • Object
show all
Defined in:
lib/easy_serializer/cacher/collection.rb

Instance Attribute Summary

Attributes inherited from Template

#metadata, #serializer

Instance Method Summary collapse

Methods inherited from Template

call, #initialize

Methods included from Helpers

#option_to_value

Constructor Details

This class inherits a constructor from EasySerializer::Cacher::Template

Instance Method Details

#block_to_get_value(elem) ⇒ Object



27
28
29
# File 'lib/easy_serializer/cacher/collection.rb', line 27

def block_to_get_value(elem)
  proc { nested_serializer.call(elem) }
end

#collectionObject



11
12
13
# File 'lib/easy_serializer/cacher/collection.rb', line 11

def collection
  @collection ||= serializer.instance_exec serializer.object, &.get_value
end

#executeObject



5
6
7
8
9
# File 'lib/easy_serializer/cacher/collection.rb', line 5

def execute
  collection.map do |elem|
    fetch(key(elem), elem)
  end
end

#fetch(key, elem) ⇒ Object



35
36
37
# File 'lib/easy_serializer/cacher/collection.rb', line 35

def fetch(key, elem)
  EasySerializer.cache.fetch(key, options, &block_to_get_value(elem))
end

#key(elem) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/easy_serializer/cacher/collection.rb', line 15

def key(elem)
  if 
    [elem, , nested_serializer.name]
  else
    [elem, nested_serializer.name]
  end.flatten
end

#nested_serializerObject



31
32
33
# File 'lib/easy_serializer/cacher/collection.rb', line 31

def nested_serializer
  .serializer(serializer.object, serializer)
end

#optionsObject



23
24
25
# File 'lib/easy_serializer/cacher/collection.rb', line 23

def options
  .cache_options || {}
end