Module: Proton::Cacheable

Included in:
Page
Defined in:
lib/proton/cacheable.rb

Overview

Module: Cache

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cache(*args) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/proton/cacheable.rb', line 19

def self.cache(*args)
  if enabled?
    @cache ||= Hash.new
    args   = args.map { |s| s.to_s }.join('-')
    @cache[args] ||= yield
  else
    yield
  end
end

.disable!Object



8
9
10
# File 'lib/proton/cacheable.rb', line 8

def self.disable!
  @enabled = false
end

.enable!Object



4
5
6
# File 'lib/proton/cacheable.rb', line 4

def self.enable!
  @enabled = true
end

.enabled?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/proton/cacheable.rb', line 12

def self.enabled?()
  !! @enabled
end

Instance Method Details

#cache_method(*methods) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/proton/cacheable.rb', line 29

def cache_method(*methods)
  methods.each do |method|
    alias_method :"real_#{method}", method

    class_eval do
      define_method(method) { |*args|
        id = nil
        id ||= self.file  if respond_to?(:file)
        id ||= self.to_s

        Cacheable.cache self.class, method, id, args do
          send :"real_#{method}", *args
        end
      }
    end
  end
end