factory_girl-cache 0.0.1
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 +102 -0
- data/Rakefile +3 -0
- data/lib/factory_girl-cache/cache.rb +48 -0
- data/lib/factory_girl-cache/version.rb +3 -0
- data/lib/factory_girl-cache.rb +2 -0
- metadata +68 -0
data/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
FactoryGirl Cache
|
|
2
|
+
=====
|
|
3
|
+
|
|
4
|
+
Instead of:
|
|
5
|
+
|
|
6
|
+
FactoryGirl.create(:post)
|
|
7
|
+
FactoryGirl.create_list(:post, 2)
|
|
8
|
+
FactoryGirl.build(:post)
|
|
9
|
+
FactoryGirl.build_list(:post, 2)
|
|
10
|
+
FactoryGirl.build_stubbed(:post)
|
|
11
|
+
|
|
12
|
+
You can use these to ensure that create, create_list, build, build_list, and build_stubbed return the same result instance(s) everytime after the first call (before the cache is cleared):
|
|
13
|
+
|
|
14
|
+
FactoryGirlCache.create(:post)
|
|
15
|
+
FactoryGirlCache.create_list(:post, 2)
|
|
16
|
+
FactoryGirlCache.build(:post)
|
|
17
|
+
FactoryGirlCache.build_list(:post, 2)
|
|
18
|
+
FactoryGirlCache.build_stubbed(:post)
|
|
19
|
+
|
|
20
|
+
To clear the cache:
|
|
21
|
+
|
|
22
|
+
FactoryGirlCache.clear
|
|
23
|
+
|
|
24
|
+
This may seem completely odd, e.g. creating something would imply really creating a new instance, as would building. And you'll get the same id back. Why would you want to do that?
|
|
25
|
+
|
|
26
|
+
By caching factory-created/built/stubbed instances and lists, you may be able to at least temporarily avoid issues with circular references and reduce time spent on nested model creation avoiding the dreaded:
|
|
27
|
+
|
|
28
|
+
SystemStackError:
|
|
29
|
+
stack level too deep
|
|
30
|
+
|
|
31
|
+
This gem should not affect FactoryGirl itself. It just provides an alternate way to use FactoryGirl that makes it behave a bit differently.
|
|
32
|
+
|
|
33
|
+
Please understand what you are doing by using this. This is not just some way to make FactoryGirl faster.
|
|
34
|
+
|
|
35
|
+
### Installation
|
|
36
|
+
|
|
37
|
+
Add to gemfile:
|
|
38
|
+
|
|
39
|
+
gem 'factory_girl-cache'
|
|
40
|
+
|
|
41
|
+
Then:
|
|
42
|
+
|
|
43
|
+
bundle install
|
|
44
|
+
|
|
45
|
+
### Usage
|
|
46
|
+
|
|
47
|
+
#### Be Sure to Clear Cache After Every Use
|
|
48
|
+
|
|
49
|
+
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!
|
|
50
|
+
|
|
51
|
+
With Rails tests:
|
|
52
|
+
|
|
53
|
+
def teardown
|
|
54
|
+
FactoryGirlCache.clear
|
|
55
|
+
# ...
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
With RSpec:
|
|
59
|
+
|
|
60
|
+
after(:each) do
|
|
61
|
+
FactoryGirlCache.clear
|
|
62
|
+
# ...
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
You can also clear cache during your tests, if it makes sense.
|
|
66
|
+
|
|
67
|
+
#### Use That Cache
|
|
68
|
+
|
|
69
|
+
If you have a post factory, then the first time you do:
|
|
70
|
+
|
|
71
|
+
FactoryGirlCache.create(:post)
|
|
72
|
+
|
|
73
|
+
It will call:
|
|
74
|
+
|
|
75
|
+
FactoryGirl.create(:post)
|
|
76
|
+
|
|
77
|
+
The second time you call it, it will just return the same instance it did before (not dup'd or anything and the id or primary key will be the same):
|
|
78
|
+
|
|
79
|
+
FactoryGirlCache.create(:post)
|
|
80
|
+
|
|
81
|
+
If you use a different method, that has a different cache, so:
|
|
82
|
+
|
|
83
|
+
FactoryGirlCache.create_list(:post, 2)
|
|
84
|
+
|
|
85
|
+
Will create two more posts. But if you call it again that will return those same two posts:
|
|
86
|
+
|
|
87
|
+
FactoryGirlCache.create_list(:post, 2)
|
|
88
|
+
|
|
89
|
+
More examples:
|
|
90
|
+
|
|
91
|
+
FactoryGirlCache.create(:post)
|
|
92
|
+
FactoryGirlCache.create_list(:post, 2)
|
|
93
|
+
FactoryGirlCache.build(:post)
|
|
94
|
+
FactoryGirlCache.build_list(:post, 2)
|
|
95
|
+
FactoryGirlCache.build_stubbed(:post)
|
|
96
|
+
|
|
97
|
+
### License
|
|
98
|
+
|
|
99
|
+
Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
|
|
100
|
+
|
|
101
|
+
[factory_girl]: https://github.com/thoughtbot/factory_girl
|
|
102
|
+
[lic]: http://github.com/garysweaver/factory_girl-cache/blob/master/LICENSE
|
data/Rakefile
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
require 'factory_girl'
|
|
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 = {}
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: factory_girl-cache
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Gary S. Weaver
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-11-06 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: factory_girl
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 2.0.0
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ! '>='
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: 2.0.0
|
|
30
|
+
description: Use FactoryGirlCache to call or retrieve cached results from FactoryGirl
|
|
31
|
+
factories.
|
|
32
|
+
email:
|
|
33
|
+
- garysweaver@gmail.com
|
|
34
|
+
executables: []
|
|
35
|
+
extensions: []
|
|
36
|
+
extra_rdoc_files: []
|
|
37
|
+
files:
|
|
38
|
+
- lib/factory_girl-cache/cache.rb
|
|
39
|
+
- lib/factory_girl-cache/version.rb
|
|
40
|
+
- lib/factory_girl-cache.rb
|
|
41
|
+
- Rakefile
|
|
42
|
+
- README.md
|
|
43
|
+
homepage: https://github.com/garysweaver/factory_girl-cache
|
|
44
|
+
licenses:
|
|
45
|
+
- MIT
|
|
46
|
+
post_install_message:
|
|
47
|
+
rdoc_options: []
|
|
48
|
+
require_paths:
|
|
49
|
+
- lib
|
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
|
+
none: false
|
|
52
|
+
requirements:
|
|
53
|
+
- - ! '>='
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
|
+
none: false
|
|
58
|
+
requirements:
|
|
59
|
+
- - ! '>='
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
requirements: []
|
|
63
|
+
rubyforge_project:
|
|
64
|
+
rubygems_version: 1.8.24
|
|
65
|
+
signing_key:
|
|
66
|
+
specification_version: 3
|
|
67
|
+
summary: Caches FactoryGirl factory results.
|
|
68
|
+
test_files: []
|