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 CHANGED
@@ -1,7 +1,11 @@
1
1
  FactoryGirl Cache
2
2
  =====
3
3
 
4
- Instead of:
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
- class FactoryGirlCache
4
-
5
- @@create_cache = {}
6
- @@create_list_cache = {}
7
- @@build_cache = {}
8
- @@build_list_cache = {}
9
- @@build_stubbed_cache = {}
10
-
11
- def self.build(class_sym, assc_sym = nil)
12
- assc_sym ||= class_sym
13
- @@build_cache[[class_sym, assc_sym]] ||= FactoryGirl.build(class_sym)
14
- end
15
-
16
- def self.build_list(class_sym, assc_sym = nil, number = nil)
17
- number ||= assc_sym
18
- assc_sym ||= class_sym
19
- number = 0 unless number.is_a? Integer
20
- @@build_list_cache[[class_sym, assc_sym, number]] ||= FactoryGirl.build_list(class_sym, number)
21
- end
22
-
23
- def self.build_stubbed(class_sym, assc_sym = nil)
24
- assc_sym ||= class_sym
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 RestfulJson
2
- VERSION = '0.0.3'
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.3
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-06 00:00:00.000000000 Z
12
+ date: 2012-11-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: factory_girl