factory_girl-cache 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -1
- data/lib/factory_girl-cache/cache.rb +22 -44
- data/lib/factory_girl-cache/version.rb +2 -2
- metadata +2 -2
data/README.md
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
FactoryGirl Cache
|
2
2
|
=====
|
3
3
|
|
4
|
-
|
4
|
+
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, with the only wierdness being that if the second argument is a symbol, it removes that from the arguments before calling FactoryGirl with the arguments and block, such that it can use different caches for different associations.
|
5
|
+
|
6
|
+
It does this with method_missing on the FactoryGirlCache module calling via `__send__`, so anything that FactoryGirl (literally, the module) can do, FactoryGirlCache should be able to do.
|
7
|
+
|
8
|
+
Just as an example, instead of:
|
5
9
|
|
6
10
|
FactoryGirl.create(:post)
|
7
11
|
FactoryGirl.create_list(:post, 2)
|
@@ -102,6 +106,8 @@ More examples:
|
|
102
106
|
|
103
107
|
#### Store Results From Same Factory for Two Different Associations Separately
|
104
108
|
|
109
|
+
Because it will use the method name and all arguments (after dup-ing) for the cache key, but will remove the second argument if ones exists after using it as the cache key, you can differentiate results from the same factory/store them in different caches.
|
110
|
+
|
105
111
|
Examples:
|
106
112
|
|
107
113
|
FactoryGirlCache.create(:post, :great_post) # will create
|
@@ -1,48 +1,26 @@
|
|
1
1
|
require 'factory_girl'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
@@build_stubbed_cache[[class_sym, assc_sym]] ||= FactoryGirl.build_stubbed(class_sym)
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.create(class_sym, assc_sym = nil)
|
29
|
-
assc_sym ||= class_sym
|
30
|
-
@@create_cache[[class_sym, assc_sym]] ||= FactoryGirl.create(class_sym)
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.create_list(class_sym, assc_sym = nil, number = nil)
|
34
|
-
number ||= assc_sym
|
35
|
-
assc_sym ||= class_sym
|
36
|
-
number = 0 unless number.is_a? Integer
|
37
|
-
@@create_list_cache[[class_sym, assc_sym, number]] ||= FactoryGirl.create_list(class_sym, number)
|
38
|
-
end
|
39
|
-
|
40
|
-
def self.clear()
|
41
|
-
@@create_cache = {}
|
42
|
-
@@create_list_cache = {}
|
43
|
-
@@build_cache = {}
|
44
|
-
@@build_list_cache = {}
|
45
|
-
@@build_stubbed_cache = {}
|
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, with the only wierdness being that if the second
|
5
|
+
# argument is a symbol, it removes that from the arguments before calling FactoryGirl with the arguments and block,
|
6
|
+
# such that it can use different caches for different associations.
|
7
|
+
module FactoryGirlCache
|
8
|
+
class << self
|
9
|
+
attr_accessor :factory_girl_cache
|
10
|
+
|
11
|
+
def method_missing(m, *args, &block)
|
12
|
+
if [:build, :build_list, :build_stubbed, :create, :create_list].include?(m)
|
13
|
+
keys = args.dup
|
14
|
+
args.delete_at(1) if args.size > 1 && args[1].is_a?(Symbol)
|
15
|
+
@factory_girl_cache ||= {}
|
16
|
+
@factory_girl_cache[[m, *keys]] ||= Math.__send__(m, *args, &block)
|
17
|
+
else
|
18
|
+
Math.__send__(m, *args, &block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def clear
|
23
|
+
@factory_girl_cache = {}
|
24
|
+
end
|
46
25
|
end
|
47
|
-
|
48
26
|
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = '0.0
|
1
|
+
module FactoryGirlCache
|
2
|
+
VERSION = '0.1.0'
|
3
3
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factory_girl-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: factory_girl
|