class MethodCacheable::MethodCache

Attributes

args[RW]
cache_operation[RW]
caller[RW]
method[RW]
options[RW]

Public Class Methods

new(caller, *method_cache_args) click to toggle source
# File lib/method_cacheable.rb, line 97
def initialize(caller, *method_cache_args)
  self.caller          = caller
  self.cache_operation = method_cache_args.map {|x| x if x.is_a? Symbol }.compact.first||:fetch
  self.options         = method_cache_args.map {|x| x if x.is_a? Hash   }.compact.first
end

Public Instance Methods

args=(args) click to toggle source
# File lib/method_cacheable.rb, line 92
def args=(args)
  args  = [args] unless args.is_a? Array
  @args = args
end
call() click to toggle source

Calls the cache based on the given cache_operation @param [Hash] options Options are passed to the cache store @see api.rubyonrails.org/classes/ActionController/Caching.html#method-i-cache Rails.cache documentation

# File lib/method_cacheable.rb, line 106
def call
  case cache_operation
  when :fetch
    MethodCacheable.store.fetch(key, options) do
      send_to_caller
    end
  when :read
    read(method, args)
  when :write
    write(method, args) do
      send_to_caller
    end
  end
end
delete(method, *args) click to toggle source

Removes the current key from the cache store

# File lib/method_cacheable.rb, line 159
def delete(method, *args)
  self.method = method
  self.args   = args
  MethodCacheable.store.delete(key, options)
end
exist?(method, *args) click to toggle source

Checks to see if the key exists in the cache store @return [boolean]

# File lib/method_cacheable.rb, line 167
def exist?(method, *args)
  self.method = method
  self.args   = args
  MethodCacheable.store.exist?(key)
end
Also aliased as: exists?
exists?(method, *args)
Alias for: exist?
for(method , *args) click to toggle source
# File lib/method_cacheable.rb, line 134
def for(method , *args)
  self.method = method
  self.args   = args
  self
end
key(tmp_method = nil, *tmp_args) click to toggle source

Uses keytar to create a key based on the method and caller if no method_key exits @see github.com/schneems/keytar Keytar, it builds keys

Method and arguement can optionally be supplied

@return the key used to set the cache @example

cache = User.find(263619).cache   # => #<MethodCacheable::MethodCache ... >
cache.method = "foo"              # => "foo"
cache.key                         # => "users:foo:263619"
# File lib/method_cacheable.rb, line 150
def key(tmp_method = nil, *tmp_args)
  tmp_method  = method if tmp_method.blank?
  tmp_args    = args   if tmp_args.blank?
  key_method = "#{tmp_method}_key".to_sym
  key = caller.send key_method, *tmp_args if caller.respond_to? key_method
  key ||= caller.build_key(:name => tmp_method, :args => tmp_args)
end
method_missing(method, *args, &blk) click to toggle source

Methods caught by method_missing are passed to the caller and used to :write, :read, or :fetch from the cache

@see MethodCacheable#cache

# File lib/method_cacheable.rb, line 177
def method_missing(method, *args, &blk)
  self.method = method
  self.args   = args
  if caller.respond_to? method
    call
  else
    send_to_caller
  end
end
read(method, *args) click to toggle source
# File lib/method_cacheable.rb, line 130
def read(method, *args)
  MethodCacheable.store.read(key, options)
end
send_to_caller() click to toggle source
# File lib/method_cacheable.rb, line 121
def send_to_caller
  caller.send method.to_sym, *args
end
write(method, *args, &block) click to toggle source
# File lib/method_cacheable.rb, line 125
def write(method, *args, &block)
  val = block.call
  MethodCacheable.store.write(key, val, options)
end