arid_cache 1.4.1 → 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ./
3
3
  specs:
4
- arid_cache (1.4.1)
4
+ arid_cache (1.4.2)
5
5
  will_paginate
6
6
 
7
7
  GEM
data/README.rdoc CHANGED
@@ -10,6 +10,8 @@ AridCache simplifies caching by supporting auto-expiring cache keys - as well as
10
10
 
11
11
  == Changes
12
12
 
13
+ * v1.4.2: Add <tt>:proxy_out</tt> and <tt>:proxy_in</tt> options; <tt>AridCache::Proxies::IdProxy</tt>. Support proxies as +Procs+.
14
+ * v1.4.1: Default <tt>:page</tt> to <tt>1</tt> if it is +nil+
13
15
  * v1.4.0: Rails 3 fully supported!
14
16
  * v1.3.5: Backwards-compatibility fixes
15
17
  * v1.3.4: Inherited cache configurations: Cache options and cache blocks are inherited from superclasses
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.4.1
1
+ 1.4.2
data/arid_cache.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{arid_cache}
8
- s.version = "1.4.1"
8
+ s.version = "1.4.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Karl Varga"]
12
- s.date = %q{2011-04-19}
12
+ s.date = %q{2011-04-21}
13
13
  s.description = %q{AridCache makes caching easy and effective. AridCache supports caching on all your model named scopes, class methods and instance methods right out of the box. AridCache prevents caching logic from cluttering your models and clarifies your logic by making explicit calls to cached result sets.
14
14
  AridCache is designed for handling large, expensive ActiveRecord collections but is equally useful for caching anything else as well.
15
15
  }
@@ -42,6 +42,7 @@ AridCache is designed for handling large, expensive ActiveRecord collections but
42
42
  "lib/arid_cache/helpers.rb",
43
43
  "lib/arid_cache/inflector.rb",
44
44
  "lib/arid_cache/inflector/inflections.rb",
45
+ "lib/arid_cache/proxies.rb",
45
46
  "lib/arid_cache/railtie.rb",
46
47
  "lib/arid_cache/store.rb",
47
48
  "rails/init.rb",
@@ -53,6 +54,7 @@ AridCache is designed for handling large, expensive ActiveRecord collections but
53
54
  "spec/arid_cache/cache_proxy/utilities_spec.rb",
54
55
  "spec/arid_cache/cache_proxy_spec.rb",
55
56
  "spec/arid_cache/helpers_spec.rb",
57
+ "spec/arid_cache/proxies_spec.rb",
56
58
  "spec/arid_cache/store_spec.rb",
57
59
  "spec/spec.opts",
58
60
  "spec/spec_helper.rb",
@@ -86,6 +88,7 @@ AridCache is designed for handling large, expensive ActiveRecord collections but
86
88
  "spec/arid_cache/cache_proxy/utilities_spec.rb",
87
89
  "spec/arid_cache/cache_proxy_spec.rb",
88
90
  "spec/arid_cache/helpers_spec.rb",
91
+ "spec/arid_cache/proxies_spec.rb",
89
92
  "spec/arid_cache/store_spec.rb",
90
93
  "spec/spec_helper.rb",
91
94
  "spec/support/ar_query.rb",
data/lib/arid_cache.rb CHANGED
@@ -8,9 +8,12 @@ require 'arid_cache/cache_proxy'
8
8
  require 'arid_cache/railtie' if defined?(Rails)
9
9
  require 'arid_cache/inflector'
10
10
  require 'arid_cache/framework'
11
+ require 'arid_cache/proxies'
11
12
 
12
13
  module AridCache
13
14
  extend AridCache::Helpers
15
+ extend AridCache::CacheProxy::Utilities
16
+
14
17
  Error = Class.new(StandardError) #:nodoc:
15
18
 
16
19
  class << self
@@ -33,7 +33,7 @@ module AridCache
33
33
  # @arg ids array of ids to order by unless an :order option is specified.
34
34
  def opts_for_find(ids)
35
35
  find_opts = reject { |k,v| !OPTIONS_FOR_FIND.include?(k) }
36
- find_opts[:order] = AridCache::CacheProxy::Utilities.order_by(ids, self[:result_klass]) unless find_opts.include?(:order)
36
+ find_opts[:order] = AridCache.order_by(ids, self[:result_klass]) unless find_opts.include?(:order)
37
37
  find_opts[:limit] = ids.size unless find_opts.include?(:limit)
38
38
  find_opts
39
39
  end
@@ -75,8 +75,13 @@ module AridCache
75
75
  include?(:order) && (self[:order].is_a?(Symbol) || self[:order].is_a?(String))
76
76
  end
77
77
 
78
- def proxy?
79
- include?(:proxy)
78
+ # Return true if the user has defined a proxy for results processing in the given
79
+ # direction.
80
+ #
81
+ # * +direction+ - :in or :out, depending on whether we are putting results into
82
+ # the cache, or returning results from the cache, respectively
83
+ def proxy?(direction)
84
+ include?(:proxy) || include?("proxy_#{direction}".to_sym)
80
85
  end
81
86
 
82
87
  def deprecated_raw?
@@ -94,6 +99,15 @@ module AridCache
94
99
  def result_klass
95
100
  fetch :result_klass
96
101
  end
102
+
103
+ # Return the user's proxy method for the given direction. Returns a symbol, Proc
104
+ # or nil if no proxy is defined.
105
+ #
106
+ # * +direction+ - :in or :out, depending on whether we are putting results into
107
+ # the cache, or returning results from the cache, respectively
108
+ def proxy(direction)
109
+ self[:proxy] || self["proxy_#{direction}".to_sym]
110
+ end
97
111
  end
98
112
  end
99
113
  end
@@ -60,11 +60,11 @@ module AridCache
60
60
  # a named scope. Calling respond_to? on an association proxy will trigger a select
61
61
  # because it loads up the target and passes the respond_to? on to it.
62
62
  @cached =
63
- if @options.proxy?
63
+ if @options.proxy?(:in)
64
64
  if is_activerecord_reflection?
65
65
  @result = @result.collect { |r| r } # force it to load
66
66
  end
67
- @options.receiver_klass.send(@options[:proxy], @result)
67
+ run_user_proxy(:in, @result)
68
68
  elsif is_activerecord_reflection?
69
69
 
70
70
  if @options.count_only?
@@ -99,7 +99,7 @@ module AridCache
99
99
  if @options.count_only?
100
100
  get_count
101
101
 
102
- elsif @options.proxy?
102
+ elsif @options.proxy?(:out)
103
103
  results =
104
104
  if @cached.nil? || !@options.raw?
105
105
  @result
@@ -107,8 +107,10 @@ module AridCache
107
107
  @cached
108
108
  end
109
109
  filtered = filter_results(results)
110
- if @cached.nil? && !@options.raw?
111
- proxy_result = @options.receiver_klass.send(@options[:proxy], filtered)
110
+ # If proxying out, we always have to proxy the result.
111
+ # If proxying both ways, we don't need to proxy the result after seeding the cache.
112
+ if !@options.raw? && (@options[:proxy_out] || @cached.nil?)
113
+ proxy_result = run_user_proxy(:out, filtered)
112
114
  if filtered.is_a?(WillPaginate::Collection) && proxy_result.is_a?(Enumerable)
113
115
  filtered.replace(proxy_result)
114
116
  else
@@ -118,6 +120,11 @@ module AridCache
118
120
  filtered
119
121
  end
120
122
 
123
+ # If proxying in, we want to return what was stored in the cache, not what was
124
+ # returned by the block. So with :proxy_in, using :raw => true has no effect.
125
+ elsif @options.proxy?(:in)
126
+ filter_results(@cached || @result)
127
+
121
128
  elsif (@cached || @result).is_a?(AridCache::CacheProxy::CachedResult) && (@cached || @result).klass == NilClass
122
129
  nil
123
130
 
@@ -145,6 +152,24 @@ module AridCache
145
152
 
146
153
  private
147
154
 
155
+ # Run the user's proxy method (or Proc) and return the result.
156
+ #
157
+ # == Arguments
158
+ # * +direction+ - :in or :out, depending on whether we are putting results into
159
+ # the cache, or returning results from the cache, respectively
160
+ # * +records+ - some kind of result that is passed to the proxy method
161
+ def run_user_proxy(direction, records)
162
+ proxy = @options.proxy(direction)
163
+ case proxy
164
+ when Symbol, String
165
+ @options.receiver_klass.send(proxy, records)
166
+ when Proc
167
+ proxy.call(records)
168
+ else
169
+ records # silently ignore it
170
+ end
171
+ end
172
+
148
173
  def get_count
149
174
  if @cached.is_a?(AridCache::CacheProxy::CachedResult) # use what we put in the cache
150
175
  @cached.count
@@ -230,7 +255,7 @@ module AridCache
230
255
  if AridCache.framework.active_record?(3)
231
256
  page_opts = @options.opts_for_paginate(ids)
232
257
  WillPaginate::Collection.create(page_opts[:page], page_opts[:per_page], page_opts[:total_entries]) do |pager|
233
- result = find_all_by_id(ids, find_opts.merge(:limit => pager.per_page, :offset => pager.offset))
258
+ result = AridCache.find_all_by_id(result_klass, ids, find_opts.merge(:limit => pager.per_page, :offset => pager.offset))
234
259
  pager.replace(result)
235
260
  end
236
261
  else
@@ -238,12 +263,12 @@ module AridCache
238
263
  result_klass.paginate(ids, find_opts)
239
264
  end
240
265
  else
241
- find_all_by_id(ids, find_opts)
266
+ AridCache.find_all_by_id(result_klass, ids, find_opts)
242
267
  end
243
268
  else
244
269
  # Limits will have already been applied, remove them from the options for find.
245
270
  [:offset, :limit].each { |key| find_opts.delete(key) }
246
- result = find_all_by_id(ids, find_opts)
271
+ result = AridCache.find_all_by_id(result_klass, ids, find_opts)
247
272
  records.is_a?(::WillPaginate::Collection) ? records.replace(result) : result
248
273
  end
249
274
  end
@@ -254,28 +279,7 @@ module AridCache
254
279
  is_cached_result? ? @result.klass : (@cached.is_a?(AridCache::CacheProxy::CachedResult) ? @cached.klass : @options[:receiver_klass])
255
280
  end
256
281
 
257
- def find_all_by_id(ids, find_opts)
258
- if AridCache.framework.active_record?(3)
259
- option_map = {
260
- :conditions => :where,
261
- :include => :includes
262
- }
263
- query = find_opts.inject(result_klass.scoped) do |scope, pair|
264
- key, value = pair
265
- key = option_map[key] || key
266
- scope.send(key, pair[1])
267
- end
268
- query = query.scoped.where(Utilities.namespaced_column(:id, result_klass) + ' in (?)', ids)
269
- # Fix http://breakthebit.org/post/3487560245/rails-3-arel-count-size-length-weirdness
270
- query.class_eval do
271
- alias_method :size, :length
272
- alias_method :count, :length
273
- end
274
- query
275
- else
276
- result_klass.find_all_by_id(ids, find_opts)
277
- end
278
- end
282
+
279
283
  end
280
284
  end
281
285
  end
@@ -35,6 +35,33 @@ module AridCache
35
35
  def object_class(object)
36
36
  object.is_a?(Class) ? object : object.class
37
37
  end
38
+
39
+ # Find and return records of the given +klass+ which have id in +ids+.
40
+ # +find_opts+ is a hash of options which are passed to find.
41
+ # If no order option is given, the ordering of the ids is preserved.
42
+ def find_all_by_id(klass, ids, find_opts={})
43
+ find_opts = Options.new(find_opts.merge(:result_klass => klass)).opts_for_find(ids)
44
+ if AridCache.framework.active_record?(3)
45
+ option_map = {
46
+ :conditions => :where,
47
+ :include => :includes
48
+ }
49
+ query = find_opts.inject(klass.scoped) do |scope, pair|
50
+ key, value = pair
51
+ key = option_map[key] || key
52
+ scope.send(key, pair[1])
53
+ end
54
+ query = query.scoped.where(Utilities.namespaced_column(:id, klass) + ' in (?)', ids)
55
+ # Fix http://breakthebit.org/post/3487560245/rails-3-arel-count-size-length-weirdness
56
+ query.class_eval do
57
+ alias_method :size, :length
58
+ alias_method :count, :length
59
+ end
60
+ query
61
+ else
62
+ klass.find_all_by_id(ids, find_opts)
63
+ end
64
+ end
38
65
  end
39
66
  end
40
- end
67
+ end
@@ -0,0 +1,18 @@
1
+ module AridCache
2
+ module Proxies
3
+ class IdProxy
4
+ # Return a Proc which takes an array. If the array contains ActiveRecords a list of
5
+ # ids is returned. If the array contains ids, the corresponding records are returned.
6
+ # All the records must be of class +klass+
7
+ def self.for(klass, find_opts={})
8
+ return Proc.new do |records|
9
+ if records.empty?
10
+ records
11
+ else
12
+ records.first.is_a?(::ActiveRecord::Base) ? records.collect(&:id) : AridCache.find_all_by_id(klass, records, find_opts)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -42,17 +42,17 @@ describe AridCache do
42
42
  AridCache.raw_with_options = true
43
43
  AridCache.raw_with_options.should be_true
44
44
  end
45
-
45
+
46
46
  describe "pagination" do
47
47
  before :each do
48
48
  @user = User.make
49
49
  @user.companies << Company.make
50
- end
51
-
50
+ end
51
+
52
52
  it "should not fail if the page is nil" do
53
53
  lambda {
54
- @user.cached_companies(:page => nil)
55
- @user.cached_companies(:page => nil) # works when seeding, so call again to load from cache
54
+ @user.cached_companies(:page => nil)
55
+ @user.cached_companies(:page => nil) # works when seeding, so call again to load from cache
56
56
  }.should_not raise_error(WillPaginate::InvalidPage)
57
57
  end
58
58
  end
@@ -109,9 +109,29 @@ describe AridCache::CacheProxy::Options do
109
109
  end
110
110
  end
111
111
 
112
- describe "proxies" do
112
+ describe "proxy?" do
113
113
  it "should use proxy" do
114
- new_options(:proxy => :serializing_proxy).proxy?.should be_true
114
+ new_options(:proxy => :xyz).proxy?(:in).should be_true
115
+ new_options(:proxy => :xyz).proxy?(:out).should be_true
116
+ end
117
+
118
+ it "should proxy in" do
119
+ new_options(:proxy_in => :xyz).proxy?(:in).should be_true
120
+ new_options(:proxy_in => :xyz).proxy?(:out).should be_false
121
+ end
122
+
123
+ it "should proxy out" do
124
+ new_options(:proxy_out => :xyz).proxy?(:in).should be_false
125
+ new_options(:proxy_out => :xyz).proxy?(:out).should be_true
126
+ end
127
+ end
128
+
129
+ describe "proxy" do
130
+ it "should return the proxy" do
131
+ new_options(:proxy_in => :xyz).proxy(:in).should == :xyz
132
+ new_options(:proxy_out => :xyz).proxy(:out).should == :xyz
133
+ new_options(:proxy => :xyz).proxy(:in).should == :xyz
134
+ new_options(:proxy => :xyz).proxy(:out).should == :xyz
115
135
  end
116
136
  end
117
137
 
@@ -8,6 +8,7 @@ describe AridCache::CacheProxy::ResultProcessor do
8
8
 
9
9
  before :each do
10
10
  AridCache.store.delete! # so no options get stored and interfere with other tests
11
+ Rails.cache.clear
11
12
  AridCache.raw_with_options = true
12
13
  end
13
14
 
@@ -227,10 +228,21 @@ describe AridCache::CacheProxy::ResultProcessor do
227
228
  before :each do
228
229
  class User
229
230
  instance_caches do
230
- companies(:proxy => :abc)
231
+ companies(:proxy => :hash_proxy)
232
+ companies_in(:proxy_in => :id_proxy) { # store ids, return ids
233
+ companies
234
+ }
235
+ companies_out(:proxy_out => :id_proxy) { # store ids, return records
236
+ companies.all.map(&:id)
237
+ }
231
238
  end
232
239
 
233
- def self.abc(records)
240
+ def self.id_proxy(records)
241
+ return records if records.empty?
242
+ records.first.is_a?(ActiveRecord::Base) ? records.collect(&:id) : AridCache.find_all_by_id(Company, records)
243
+ end
244
+
245
+ def self.hash_proxy(records)
234
246
  return records if records.empty?
235
247
  records.first.is_a?(ActiveRecord::Base) ? records.collect(&:attributes) : records.collect { |r| Company.find_by_id(r['id']) }
236
248
  end
@@ -243,19 +255,26 @@ describe AridCache::CacheProxy::ResultProcessor do
243
255
  end
244
256
 
245
257
  it "the proxy called on itself should return the original value" do
246
- User.abc(User.abc([@company])).should == [@company]
258
+ User.hash_proxy(User.hash_proxy([@company])).should == [@company]
259
+ end
260
+
261
+ it "the proxy called on itself should return the original value" do
262
+ User.id_proxy(User.id_proxy([@company])).should == [@company]
247
263
  end
248
264
 
249
265
  it "should serialize" do
266
+ mock.proxy(User).hash_proxy(anything)
250
267
  @user.cached_companies.should == [@company]
251
268
  end
252
269
 
253
270
  it "should un-serialize" do
254
- @user.cached_companies # seed the cache
271
+ mock.proxy(User).hash_proxy(anything).twice
272
+ @user.cached_companies.should == [@company] # seed the cache
255
273
  @user.cached_companies.should == [@company]
256
274
  end
257
275
 
258
276
  it "should return json with :raw => true" do
277
+ mock.proxy(User).hash_proxy(anything)
259
278
  # Seed the cache; it should use the serialized result in the return value.
260
279
  value = @user.cached_companies(:raw => true)
261
280
 
@@ -268,7 +287,7 @@ describe AridCache::CacheProxy::ResultProcessor do
268
287
  end
269
288
 
270
289
  # Cache is seeded, it should use the cached result
271
- dont_allow(User).abc
290
+ dont_allow(User).hash_proxy
272
291
  value = @user.cached_companies(:raw => true)
273
292
  value.should be_a(Array)
274
293
  value.first.should be_a(Hash)
@@ -277,6 +296,56 @@ describe AridCache::CacheProxy::ResultProcessor do
277
296
  end
278
297
  end
279
298
 
299
+ it "should proxy out only" do
300
+ mock.proxy(User).id_proxy(anything)
301
+ @user.cached_companies_out.should == @user.companies.to_a
302
+ @user.cached_companies_out(:raw => true).should == @user.companies.map(&:id)
303
+ end
304
+
305
+ it "should proxy in only" do
306
+ mock.proxy(User).id_proxy(anything)
307
+ @user.cached_companies_in.should == @user.companies.map(&:id)
308
+ @user.cached_companies_in(:raw => true).should == @user.companies.map(&:id)
309
+ end
310
+
311
+ describe "as a Proc" do
312
+ before :each do
313
+ @ids = [1,2,3]
314
+ @proc = Proc.new { |records| records.reverse }
315
+ end
316
+
317
+ it "the proc should reverse the list" do
318
+ #mock.proxy(@proxy).call(@ids)
319
+ @proc.call(@ids).should == @ids.reverse
320
+ end
321
+
322
+ it "should be called going in" do
323
+ #mock.proxy(@proxy).call(@ids)
324
+ new_result(nil, :proxy_in => @proc).send(:run_user_proxy, :in, @ids).should == @ids.reverse
325
+ end
326
+
327
+ it "should not be called going out" do
328
+ #mock.proxy(@proxy).call(@ids).never
329
+ new_result(nil, :proxy_in => @proc).send(:run_user_proxy, :out, @ids).should == @ids
330
+ end
331
+
332
+ it "should be called going out" do
333
+ #mock.proxy(@proxy).call(@ids)
334
+ new_result(nil, :proxy_out => @proc).send(:run_user_proxy, :out, @ids).should == @ids.reverse
335
+ end
336
+
337
+ it "should not be called going in" do
338
+ #mock.proxy(@proxy).call(@ids).never
339
+ new_result(nil, :proxy_out => @proc).send(:run_user_proxy, :in, @ids).should == @ids
340
+ end
341
+
342
+ it "should be called both ways" do
343
+ #mock.proxy(@proxy).call(anything).twice
344
+ new_result(nil, :proxy => @proc).send(:run_user_proxy, :in, @ids).should == @ids.reverse
345
+ new_result(nil, :proxy => @proc).send(:run_user_proxy, :out, @ids).should == @ids.reverse
346
+ end
347
+ end
348
+
280
349
  describe "with options" do
281
350
  before :each do
282
351
  @c2 = Company.make
@@ -292,7 +361,7 @@ describe AridCache::CacheProxy::ResultProcessor do
292
361
 
293
362
  it "should apply limit and offset with :raw => true" do
294
363
  @companies = @user.cached_companies
295
- @user.cached_companies(:limit => 2, :offset => 1, :raw => true).should == User.abc(@companies[1,2])
364
+ @user.cached_companies(:limit => 2, :offset => 1, :raw => true).should == User.hash_proxy(@companies[1,2])
296
365
  end
297
366
 
298
367
  it "should apply pagination" do
@@ -312,7 +381,7 @@ describe AridCache::CacheProxy::ResultProcessor do
312
381
  end
313
382
 
314
383
  it "should paginate with :raw => true" do
315
- @companies = User.abc(@user.cached_companies)
384
+ @companies = User.hash_proxy(@user.cached_companies)
316
385
  value = @user.cached_companies(:page => 2, :per_page => 2, :raw => true)
317
386
  value.should be_a(WillPaginate::Collection)
318
387
  value.first.should be_a(Hash)
@@ -482,24 +551,6 @@ describe AridCache::CacheProxy::ResultProcessor do
482
551
  end
483
552
  end
484
553
 
485
- describe "find_all_by_id" do
486
- before :each do
487
- @user = User.make
488
- @user.companies << Company.make
489
- @user.companies << Company.make
490
- Company.make # there must be more than 2 companies for it to fail
491
- @cached = AridCache::CacheProxy::CachedResult.new
492
- @cached.ids = @user.companies.reverse.map(&:id)
493
- @cached.klass = Company
494
- @result = new_result(@cached)
495
- mock.proxy(@result).find_all_by_id(@cached.ids, anything)
496
- end
497
-
498
- it "should maintain order" do
499
- @result.to_result.should == @user.companies.reverse
500
- end
501
- end
502
-
503
554
  describe "result_klass" do
504
555
  before :each do
505
556
  @obj = Class.new do
@@ -4,12 +4,35 @@ describe AridCache::CacheProxy::Utilities do
4
4
  describe 'order_by' do
5
5
  it "id column should be prefixed by the table name" do
6
6
  stub(::ActiveRecord::Base).is_mysql_adapter? { true }
7
- AridCache::CacheProxy::Utilities.order_by([1,2,3], Company).should =~ %r[#{Company.table_name}]
7
+ AridCache.order_by([1,2,3], Company).should =~ %r[#{Company.table_name}]
8
8
  end
9
9
 
10
10
  it "id column should be prefixed by the table name" do
11
11
  stub(::ActiveRecord::Base).is_mysql_adapter? { false }
12
- AridCache::CacheProxy::Utilities.order_by([1,2,3], Company).should =~ %r[#{Company.table_name}]
12
+ AridCache.order_by([1,2,3], Company).should =~ %r[#{Company.table_name}]
13
+ end
14
+ end
15
+
16
+ describe "find_all_by_id" do
17
+ before :each do
18
+ @user = User.make
19
+ @user.companies << Company.make
20
+ @user.companies << Company.make
21
+ Company.make # there must be more than 2 companies for it to fail
22
+ end
23
+
24
+ it "should maintain order" do
25
+ @result = AridCache::CacheProxy::Utilities.find_all_by_id(Company, @user.companies.reverse.map(&:id))
26
+ @result.should == @user.companies.reverse
27
+ end
28
+
29
+ it "should apply options" do
30
+ @result = AridCache::CacheProxy::Utilities.find_all_by_id(Company, @user.companies.reverse.map(&:id),
31
+ :limit => 1,
32
+ :offset => 1
33
+ )
34
+ @result.size.should == 1
35
+ @result.first.should == @user.companies.reverse[1]
13
36
  end
14
37
  end
15
38
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe AridCache::Proxies::IdProxy do
4
+ before :each do
5
+ @user = User.make
6
+ @user.companies << Company.make
7
+ @ids = @user.companies.map(&:id)
8
+ end
9
+
10
+ it "should return ids given records" do
11
+ AridCache::Proxies::IdProxy.for(Company).call(@user.companies).should == @ids
12
+ end
13
+
14
+ it "should return records given ids" do
15
+ AridCache::Proxies::IdProxy.for(Company).call(@ids).should == @user.companies.to_a
16
+ end
17
+
18
+ it "should accept options to find" do
19
+ result = AridCache::Proxies::IdProxy.for(Company, :order => 'id DESC', :include => :owner).call(@ids)
20
+ result.should == @user.companies.reverse
21
+ lambda {
22
+ result.collect(&:owner)
23
+ }.should query(0)
24
+ end
25
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arid_cache
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 4
9
- - 1
10
- version: 1.4.1
9
+ - 2
10
+ version: 1.4.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Karl Varga
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-19 00:00:00 -07:00
18
+ date: 2011-04-21 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -110,6 +110,7 @@ files:
110
110
  - lib/arid_cache/helpers.rb
111
111
  - lib/arid_cache/inflector.rb
112
112
  - lib/arid_cache/inflector/inflections.rb
113
+ - lib/arid_cache/proxies.rb
113
114
  - lib/arid_cache/railtie.rb
114
115
  - lib/arid_cache/store.rb
115
116
  - rails/init.rb
@@ -121,6 +122,7 @@ files:
121
122
  - spec/arid_cache/cache_proxy/utilities_spec.rb
122
123
  - spec/arid_cache/cache_proxy_spec.rb
123
124
  - spec/arid_cache/helpers_spec.rb
125
+ - spec/arid_cache/proxies_spec.rb
124
126
  - spec/arid_cache/store_spec.rb
125
127
  - spec/spec.opts
126
128
  - spec/spec_helper.rb
@@ -182,6 +184,7 @@ test_files:
182
184
  - spec/arid_cache/cache_proxy/utilities_spec.rb
183
185
  - spec/arid_cache/cache_proxy_spec.rb
184
186
  - spec/arid_cache/helpers_spec.rb
187
+ - spec/arid_cache/proxies_spec.rb
185
188
  - spec/arid_cache/store_spec.rb
186
189
  - spec/spec_helper.rb
187
190
  - spec/support/ar_query.rb