factory_girl-cache 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -0
- data/lib/factory_girl-cache/cache.rb +8 -3
- data/lib/factory_girl-cache/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -63,6 +63,14 @@ With RSpec:
|
|
63
63
|
# ...
|
64
64
|
end
|
65
65
|
|
66
|
+
Even better, in RSpec you can clear cache after every test by adding this to `spec/spec_helper.rb`:
|
67
|
+
|
68
|
+
Spec::Runner.configure do |config|
|
69
|
+
config.after(:each) do
|
70
|
+
FactoryGirlCache.clear
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
66
74
|
You can also clear cache during your tests, if it makes sense.
|
67
75
|
|
68
76
|
#### Use That Cache
|
@@ -117,6 +125,13 @@ For example:
|
|
117
125
|
FactoryGirlCache.build_stubbed(:post, cached_as: :great_post) # will return post from cache
|
118
126
|
FactoryGirlCache.build_stubbed(:post, cached_as: :bad_post) # will build stubbed
|
119
127
|
|
128
|
+
#### Completely Override Cache Key
|
129
|
+
|
130
|
+
If the objects are equivalent, you can do this:
|
131
|
+
|
132
|
+
FactoryGirlCache.create(:bar, cache_key: :bird) # will create
|
133
|
+
FactoryGirlCache.create(:foo, cached_as: :bird) # will return bar from cache
|
134
|
+
|
120
135
|
### License
|
121
136
|
|
122
137
|
Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
|
@@ -11,10 +11,15 @@ module FactoryGirlCache
|
|
11
11
|
if [:build, :build_list, :build_stubbed, :create, :create_list].include?(m)
|
12
12
|
keys = args.dup
|
13
13
|
options = args.last
|
14
|
-
|
15
|
-
|
14
|
+
cache_key = options.is_a?(Hash) ? options.delete(:cache_key) : nil
|
15
|
+
unless cache_key
|
16
|
+
cached_as = options.is_a?(Hash) ? options.delete(:cached_as) : nil
|
17
|
+
keys[0] = cached_as if !(cached_as.nil?) && keys.size > 0
|
18
|
+
# avoid issues with different object_id's on option hashes etc. being considered new arguments
|
19
|
+
cache_key = [m, *keys].inspect.to_sym
|
20
|
+
end
|
16
21
|
@factory_girl_cache ||= {}
|
17
|
-
@factory_girl_cache[
|
22
|
+
@factory_girl_cache[cache_key] ||= FactoryGirl.__send__(m, *args, &block)
|
18
23
|
else
|
19
24
|
FactoryGirl.__send__(m, *args, &block)
|
20
25
|
end
|