Module: FactoryGirlCache

Defined in:
lib/factory_girl-cache/cache.rb,
lib/factory_girl-cache/version.rb

Overview

A wrapper for FactoryGirl that caches the build, build_list, build_stubbed, create, and create_list methods using the method (as symbol) and arguments as the key for the cache. You can send in a :cached_as option and it will use that in place of the factory name/1st argument in the key of the cache.

Constant Summary collapse

VERSION =
'0.3.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.factory_girl_cacheObject

Returns the value of attribute factory_girl_cache.



8
9
10
# File 'lib/factory_girl-cache/cache.rb', line 8

def factory_girl_cache
  @factory_girl_cache
end

Class Method Details

.clearObject



28
29
30
# File 'lib/factory_girl-cache/cache.rb', line 28

def clear
  @factory_girl_cache = {}
end

.method_missing(m, *args, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/factory_girl-cache/cache.rb', line 10

def method_missing(m, *args, &block)
  if [:build, :build_list, :build_stubbed, :create, :create_list].include?(m)
    keys = args.dup
    options = args.last
    cache_key = options.is_a?(Hash) ? options.delete(:cache_key) : nil
    unless cache_key
      cached_as = options.is_a?(Hash) ? options.delete(:cached_as) : nil
      keys[0] = cached_as if !(cached_as.nil?) && keys.size > 0
      # avoid issues with different object_id's on option hashes etc. being considered new arguments
      cache_key = [m, *keys].inspect.to_sym
    end
    @factory_girl_cache ||= {}
    @factory_girl_cache[cache_key] ||= FactoryGirl.__send__(m, *args, &block)
  else
    FactoryGirl.__send__(m, *args, &block)
  end
end