Class: Caching::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/caching/proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(target, *methods) ⇒ Proxy

Returns a new instance of Proxy.



6
7
8
9
10
# File 'lib/caching/proxy.rb', line 6

def initialize(target, *methods)
  @target = target
  @methods = methods.map(&:to_sym)
  @storage = Storage.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/caching/proxy.rb', line 12

def method_missing(method, *args, &block)
  if @methods.include? method.to_sym
    @storage.fetch(method) { @target.send(method, *args, &block) }
  else
    @target.send(method, *args, &block)
  end
end

Instance Method Details

#clear_cache(*methods) ⇒ Object



20
21
22
# File 'lib/caching/proxy.rb', line 20

def clear_cache(*methods)
  @storage.clear *methods
end