factory_girl-cache 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +19 -25
- data/lib/factory_girl-cache/cache.rb +5 -4
- data/lib/factory_girl-cache/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
|
@@ -45,12 +45,6 @@ Then:
|
|
|
45
45
|
|
|
46
46
|
### Usage
|
|
47
47
|
|
|
48
|
-
#### Require
|
|
49
|
-
|
|
50
|
-
You may need to add this to the top of the test or factories.rb or wherever you are going to use it:
|
|
51
|
-
|
|
52
|
-
require 'factory_girl-cache'
|
|
53
|
-
|
|
54
48
|
#### Be Sure to Clear Cache After Every Use
|
|
55
49
|
|
|
56
50
|
In your tests, be sure that the cache is cleared after anything that uses it is done, otherwise it probably will eventually pollute other tests if you are not careful!
|
|
@@ -103,25 +97,25 @@ More examples:
|
|
|
103
97
|
|
|
104
98
|
#### Store Results From Same Factory for Two Different Associations Separately
|
|
105
99
|
|
|
106
|
-
Because it will use the method name and all arguments
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
FactoryGirlCache.create(:post, :great_post) # will create
|
|
111
|
-
FactoryGirlCache.create(:post, :great_post) # will return post from cache
|
|
112
|
-
FactoryGirlCache.create(:post, :bad_post) # will create
|
|
113
|
-
FactoryGirlCache.create_list(:post, :great_posts
|
|
114
|
-
FactoryGirlCache.create_list(:post, :bad_posts
|
|
115
|
-
FactoryGirlCache.create_list(:post, :bad_posts
|
|
116
|
-
FactoryGirlCache.build(:post, :great_post) # will build
|
|
117
|
-
FactoryGirlCache.build(:post, :great_post) # will return post from cache
|
|
118
|
-
FactoryGirlCache.build(:post, :bad_post) # will build
|
|
119
|
-
FactoryGirlCache.build_list(:post, :great_posts
|
|
120
|
-
FactoryGirlCache.build_list(:post, :bad_posts
|
|
121
|
-
FactoryGirlCache.build_list(:post, :bad_posts
|
|
122
|
-
FactoryGirlCache.build_stubbed(:post, :great_post) # will build stubbed
|
|
123
|
-
FactoryGirlCache.build_stubbed(:post, :great_post) # will return post from cache
|
|
124
|
-
FactoryGirlCache.build_stubbed(:post, :bad_post) # will build stubbed
|
|
100
|
+
Because it will use the method name and all arguments for the cache key, but will look for and remove the option `:cached_as` and used that instead of the factory name in the cache key.
|
|
101
|
+
|
|
102
|
+
For example:
|
|
103
|
+
|
|
104
|
+
FactoryGirlCache.create(:post, cached_as: :great_post) # will create
|
|
105
|
+
FactoryGirlCache.create(:post, cached_as: :great_post) # will return post from cache
|
|
106
|
+
FactoryGirlCache.create(:post, cached_as: :bad_post) # will create
|
|
107
|
+
FactoryGirlCache.create_list(:post, 2, cached_as: :great_posts) # will create list of 2
|
|
108
|
+
FactoryGirlCache.create_list(:post, 2, cached_as: :bad_posts) # will create list of 2
|
|
109
|
+
FactoryGirlCache.create_list(:post, 2, cached_as: :bad_posts) # will return list of 2 from cache
|
|
110
|
+
FactoryGirlCache.build(:post, cached_as: :great_post) # will build
|
|
111
|
+
FactoryGirlCache.build(:post, cached_as: :great_post) # will return post from cache
|
|
112
|
+
FactoryGirlCache.build(:post, cached_as: :bad_post) # will build
|
|
113
|
+
FactoryGirlCache.build_list(:post, 2, cached_as: :great_posts) # will build list of 2
|
|
114
|
+
FactoryGirlCache.build_list(:post, 2, cached_as: :bad_posts) # will build list of 2
|
|
115
|
+
FactoryGirlCache.build_list(:post, 2, cached_as: :bad_posts) # will return list of 2 from cache
|
|
116
|
+
FactoryGirlCache.build_stubbed(:post, cached_as: :great_post) # will build stubbed
|
|
117
|
+
FactoryGirlCache.build_stubbed(:post, cached_as: :great_post) # will return post from cache
|
|
118
|
+
FactoryGirlCache.build_stubbed(:post, cached_as: :bad_post) # will build stubbed
|
|
125
119
|
|
|
126
120
|
### License
|
|
127
121
|
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
require 'factory_girl'
|
|
2
2
|
|
|
3
3
|
# A wrapper for FactoryGirl that caches the build, build_list, build_stubbed, create, and create_list methods using
|
|
4
|
-
# the method (as symbol) and arguments as the key for the cache
|
|
5
|
-
#
|
|
6
|
-
# such that it can use different caches for different associations.
|
|
4
|
+
# the method (as symbol) and arguments as the key for the cache. You can send in a :cached_as option and it will
|
|
5
|
+
# use that in place of the factory name/1st argument in the key of the cache.
|
|
7
6
|
module FactoryGirlCache
|
|
8
7
|
class << self
|
|
9
8
|
attr_accessor :factory_girl_cache
|
|
@@ -11,7 +10,9 @@ module FactoryGirlCache
|
|
|
11
10
|
def method_missing(m, *args, &block)
|
|
12
11
|
if [:build, :build_list, :build_stubbed, :create, :create_list].include?(m)
|
|
13
12
|
keys = args.dup
|
|
14
|
-
|
|
13
|
+
options = args.last
|
|
14
|
+
cached_as = options.is_a?(Hash) ? options.remove(:cached_as) : nil
|
|
15
|
+
keys[0] = cached_as if !(cached_as.nil?) && keys.size > 0
|
|
15
16
|
@factory_girl_cache ||= {}
|
|
16
17
|
@factory_girl_cache[[m, *keys]] ||= FactoryGirl.__send__(m, *args, &block)
|
|
17
18
|
else
|