factory_girl-cache 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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 (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.
107
-
108
- Examples:
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, 2) # will create list of 2
114
- FactoryGirlCache.create_list(:post, :bad_posts, 2) # will create list of 2
115
- FactoryGirlCache.create_list(:post, :bad_posts, 2) # will return list of 2 from cache
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, 2) # will build list of 2
120
- FactoryGirlCache.build_list(:post, :bad_posts, 2) # will build list of 2
121
- FactoryGirlCache.build_list(:post, :bad_posts, 2) # will return list of 2 from cache
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, 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.
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
- args.delete_at(1) if args.size > 1 && args[1].is_a?(Symbol)
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
@@ -1,3 +1,3 @@
1
1
  module FactoryGirlCache
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.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.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: