Class: Cash::Local

Inherits:
Object
  • Object
show all
Defined in:
lib/cash/local.rb

Instance Method Summary collapse

Constructor Details

#initialize(remote_cache) ⇒ Local

Returns a new instance of Local.



3
4
5
# File 'lib/cash/local.rb', line 3

def initialize(remote_cache)
  @remote_cache = remote_cache
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



31
32
33
34
35
# File 'lib/cash/local.rb', line 31

def method_missing(method, *args, &block)
  autoload_missing_constants do
    @remote_cache.send(method, *args, &block)
  end
end

Instance Method Details

#autoload_missing_constantsObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/cash/local.rb', line 14

def autoload_missing_constants
  yield if block_given?
rescue ArgumentError, *@remote_cache.exception_classes => error
  lazy_load ||= Hash.new { |hash, hash_key| hash[hash_key] = true; false }
  if error.to_s[/undefined class|referred/] && !lazy_load[error.to_s.split.last.constantize]
    retry
  else
    raise error
  end
end

#cache_locallyObject



7
8
9
10
11
12
# File 'lib/cash/local.rb', line 7

def cache_locally
  @remote_cache = LocalBuffer.new(original_cache = @remote_cache)
  yield if block_given?
ensure
  @remote_cache = original_cache
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/cash/local.rb', line 25

def respond_to?(method)
  super || @remote_cache.respond_to?(method)
end