Module: AridCache::Helpers

Included in:
AridCache
Defined in:
lib/arid_cache/helpers.rb

Instance Method Summary collapse

Instance Method Details

#define(object, key, opts, fetch_method = :fetch, method_name = nil, &block) ⇒ Object

Store the options and optional block for a call to the cache.

If no block is provided, create one dynamically.

Returns:

  • an AridCache::Store::Blueprint.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/arid_cache/helpers.rb', line 42

def define(object, key, opts, fetch_method=:fetch, method_name=nil, &block)

  # FIXME: Pass default options to store.add
  # Pass nil in for now until we get the cache_ calls working.
  # This means that the first time you define a dynamic cache
  # (by passing in a block), the options you used are not
  # stored in the blueprint and applied to each subsequent call.
  #
  # Otherwise we have a situation where a :limit passed in to the
  # first call persists when no options are passed in on subsequent calls,
  # but if a different :limit is passed in that limit is applied.
  #
  # I think in this scenario one would expect no limit to be applied
  # if no options are passed in.
  #
  # When the cache_ methods are supported, those options should be
  # remembered and applied to the collection however.
  blueprint = AridCache.store.add_object_cache_configuration(object, key, nil, block)
  method_for_cached(object, key, fetch_method, method_name)
  blueprint
end

#lookup(object, key, opts, &block) ⇒ Object

Lookup something from the cache.

If no block is provided, create one dynamically. If a block is provided, it is only used the first time it is encountered. This allows you to dynamically define your caches while still returning the results of your query.

Returns:

  • a WillPaginate::Collection if the options include :page, a Fixnum count if the request is for a count or the results of the ActiveRecord query otherwise.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/arid_cache/helpers.rb', line 14

def lookup(object, key, opts, &block)
  if !block.nil?
    define(object, key, opts, &block)
  elsif key =~ /(.*)_count$/
    if AridCache.store.has?(object, $1)
      method_for_cached(object, $1, :fetch_count, key)
    elsif object.respond_to?(key)
      define(object, key, opts, :fetch_count)
    elsif object.respond_to?($1)
      define(object, $1, opts, :fetch_count, key)
    else
      raise ArgumentError.new("#{object} doesn't respond to #{key} or #{$1}!  Cannot dynamically create query to get the count, please call with a block.")
    end
  elsif AridCache.store.has?(object, key)
    method_for_cached(object, key, :fetch)
  elsif object.respond_to?(key)
    define(object, key, opts, &block)
  else
    raise ArgumentError.new("#{object} doesn't respond to #{key}!  Cannot dynamically create query, please call with a block.")
  end
  object.send("cached_#{key}", opts)
end

#subclasses_of(parent) ⇒ Object



64
65
66
67
68
# File 'lib/arid_cache/helpers.rb', line 64

def subclasses_of(parent)
  result = []
  ObjectSpace.each_object(Class) { |klass| result << klass if klass < parent }
  result
end