Class: EasySerializer::RootCacher

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/easy_serializer/root_cacher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#option_to_value

Constructor Details

#initialize(serializer, &block) ⇒ RootCacher

Returns a new instance of RootCacher.



10
11
12
13
# File 'lib/easy_serializer/root_cacher.rb', line 10

def initialize(serializer, &block)
  @serializer = serializer
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



9
10
11
# File 'lib/easy_serializer/root_cacher.rb', line 9

def block
  @block
end

#serializerObject (readonly)

Returns the value of attribute serializer.



9
10
11
# File 'lib/easy_serializer/root_cacher.rb', line 9

def serializer
  @serializer
end

Class Method Details

.call(serializer, &block) ⇒ Object



5
6
7
# File 'lib/easy_serializer/root_cacher.rb', line 5

def self.call(serializer, &block)
  new(serializer, &block).execute
end

Instance Method Details

#executeObject



15
16
17
# File 'lib/easy_serializer/root_cacher.rb', line 15

def execute
  fetch
end

#fetchObject



41
42
43
# File 'lib/easy_serializer/root_cacher.rb', line 41

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

#keyObject



31
32
33
34
35
36
37
38
39
# File 'lib/easy_serializer/root_cacher.rb', line 31

def key
  custom_key = serializer.__cache[:key]
  if custom_key
    k = option_to_value(custom_key, object, serializer)
    [object, k, serializer.class.name]
  else
    [object, serializer.class.name]
  end.flatten
end

#objectObject



19
20
21
# File 'lib/easy_serializer/root_cacher.rb', line 19

def object
  serializer.object
end

#optionsObject



23
24
25
26
27
28
29
# File 'lib/easy_serializer/root_cacher.rb', line 23

def options
  h = serializer.__cache.dup
  [:block, :cache_key, :root_call, :serializer, :key].each do |k|
    h.delete(k)
  end
  h
end