factory_girl-cache 0.2.1 → 0.3.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 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
- cached_as = options.is_a?(Hash) ? options.delete(:cached_as) : nil
15
- keys[0] = cached_as if !(cached_as.nil?) && keys.size > 0
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[[m, *keys]] ||= FactoryGirl.__send__(m, *args, &block)
22
+ @factory_girl_cache[cache_key] ||= FactoryGirl.__send__(m, *args, &block)
18
23
  else
19
24
  FactoryGirl.__send__(m, *args, &block)
20
25
  end
@@ -1,3 +1,3 @@
1
1
  module FactoryGirlCache
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.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.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: