Class: CacheMethod::Generation

Inherits:
Object
  • Object
show all
Defined in:
lib/cache_method/generation.rb

Overview

:nodoc: all

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, method_id) ⇒ Generation

Returns a new instance of Generation.



10
11
12
13
# File 'lib/cache_method/generation.rb', line 10

def initialize(obj, method_id)
  @obj = obj
  @method_id = method_id
end

Instance Attribute Details

#method_idObject (readonly)

Returns the value of attribute method_id.



16
17
18
# File 'lib/cache_method/generation.rb', line 16

def method_id
  @method_id
end

#objObject (readonly)

Returns the value of attribute obj.



15
16
17
# File 'lib/cache_method/generation.rb', line 15

def obj
  @obj
end

Class Method Details

.random_nameObject



5
6
7
# File 'lib/cache_method/generation.rb', line 5

def random_name
  rand(100_000_000).to_s
end

Instance Method Details

#cache_keyObject



26
27
28
29
30
31
32
# File 'lib/cache_method/generation.rb', line 26

def cache_key
  if obj.is_a?(::Class) or obj.is_a?(::Module)
    [ 'CacheMethod', 'Generation', method_signature ].join ','
  else
    [ 'CacheMethod', 'Generation', method_signature, obj_digest ].join ','
  end
end

#currentObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/cache_method/generation.rb', line 34

def current
  if cached_v = Config.instance.storage.get(cache_key)
    cached_v
  else
    v = Generation.random_name
    # never expire!
    Config.instance.storage.set cache_key, v, 0
    v
  end
end

#mark_passingObject



45
46
47
# File 'lib/cache_method/generation.rb', line 45

def mark_passing
  Config.instance.storage.delete cache_key
end

#method_signatureObject



18
19
20
# File 'lib/cache_method/generation.rb', line 18

def method_signature
  @method_signature ||= ::CacheMethod.method_signature(obj, method_id)
end

#obj_digestObject



22
23
24
# File 'lib/cache_method/generation.rb', line 22

def obj_digest
  @obj_digest ||= ::Digest::SHA1.hexdigest(::Marshal.dump(obj.respond_to?(:as_cache_key) ? obj.as_cache_key : obj))
end