arid_cache 1.4.3 → 1.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ./
3
3
  specs:
4
- arid_cache (1.4.2)
4
+ arid_cache (1.4.4)
5
5
  will_paginate
6
6
 
7
7
  GEM
@@ -10,6 +10,7 @@ AridCache simplifies caching by supporting auto-expiring cache keys - as well as
10
10
 
11
11
  == Changes
12
12
 
13
+ * v1.4.4: Fix empty relations return nil instead of an empty array
13
14
  * v1.4.2: Add <tt>:proxy_out</tt> and <tt>:proxy_in</tt> options; <tt>AridCache::Proxies::IdProxy</tt>. Support proxies as +Procs+.
14
15
  * v1.4.1: Default <tt>:page</tt> to <tt>1</tt> if it is +nil+
15
16
  * v1.4.0: Rails 3 fully supported!
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.4.3
1
+ 1.4.4
@@ -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.3"
8
+ s.version = "1.4.4"
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-21}
12
+ s.date = %q{2011-04-27}
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
  }
@@ -12,6 +12,7 @@ module AridCache
12
12
  # into that class. We have to keep whatever is cached as small as possible tho,
13
13
  # so it's probably best to cache a Hash and load it with CachedResult.
14
14
  class ResultProcessor
15
+ attr_reader :options
15
16
 
16
17
  def initialize(result, opts={})
17
18
  @result = result
@@ -65,13 +66,12 @@ module AridCache
65
66
  @result = @result.collect { |r| r } # force it to load
66
67
  end
67
68
  run_user_proxy(:in, @result)
68
- elsif is_activerecord_reflection?
69
-
69
+ elsif is_activerecord_reflection? # Don't trigger it unless we really have to
70
70
  if @options.count_only?
71
71
  lazy_cache.count = @result.count
72
72
  else
73
73
  lazy_cache.ids = @result.collect { |r| r[:id] }
74
- lazy_cache.klass = @result.respond_to?(:proxy_reflection) ? @result.proxy_reflection.klass : (is_empty? ? result_klass : @result.first.class)
74
+ lazy_cache.klass = Utilities.collection_klass(@result) || result_klass
75
75
  lazy_cache.count = @result.size
76
76
  end
77
77
  lazy_cache
@@ -95,7 +95,7 @@ module AridCache
95
95
 
96
96
  # Apply any options like pagination or ordering and return the result, which
97
97
  # is either some base type, or usually, a list of active records.
98
- def to_result
98
+ def to_result
99
99
  if @options.count_only?
100
100
  get_count
101
101
 
@@ -125,7 +125,7 @@ module AridCache
125
125
  elsif @options.proxy?(:in)
126
126
  filter_results(@cached || @result)
127
127
 
128
- elsif (@cached || @result).is_a?(AridCache::CacheProxy::CachedResult) && (@cached || @result).klass == NilClass
128
+ elsif (@cached || @result).is_a?(AridCache::CacheProxy::CachedResult) && (@cached || @result).klass == NilClass && !(@cached || @result).has_ids?
129
129
  nil
130
130
 
131
131
  elsif @options.raw?
@@ -278,8 +278,6 @@ module AridCache
278
278
  def result_klass
279
279
  is_cached_result? ? @result.klass : (@cached.is_a?(AridCache::CacheProxy::CachedResult) ? @cached.klass : @options[:receiver_klass])
280
280
  end
281
-
282
-
283
281
  end
284
282
  end
285
283
  end
@@ -64,6 +64,20 @@ module AridCache
64
64
  klass.find_all_by_id(ids, find_opts)
65
65
  end
66
66
  end
67
+
68
+ # Infer the class of the objects in a collection. The collection could be empty.
69
+ # If the collection is a relation or proxy reflection we can get the class from it.
70
+ #
71
+ # Return the class or nil if no class can be inferred.
72
+ def collection_klass(collection)
73
+ if collection.respond_to?(:proxy_reflection)
74
+ collection.proxy_reflection.klass
75
+ elsif defined?(::ActiveRecord::Relation) && collection.is_a?(::ActiveRecord::Relation)
76
+ collection.klass
77
+ elsif collection.respond_to?(:first) && collection.first
78
+ collection.first.class
79
+ end
80
+ end
67
81
  end
68
82
  end
69
83
  end
@@ -6,6 +6,16 @@ describe AridCache::CacheProxy::ResultProcessor do
6
6
  AridCache::CacheProxy::ResultProcessor.new(value, opts)
7
7
  end
8
8
 
9
+ # Yield the block once with raw_with_options off, and once with raw_with_options on.
10
+ # The block should make RSpec assertions.
11
+ def with_deprecated_support
12
+ current = AridCache.raw_with_options
13
+ yield
14
+ AridCache.raw_with_options = !!current
15
+ yield
16
+ AridCache.raw_with_options = current
17
+ end
18
+
9
19
  before :each do
10
20
  AridCache.raw_with_options = true
11
21
  end
@@ -491,9 +501,9 @@ describe AridCache::CacheProxy::ResultProcessor do
491
501
  new_result(@obj).is_activerecord_reflection?.should be_true
492
502
  end
493
503
 
494
- it "should set klass" do
504
+ it "should not be able to infer the result klass" do
495
505
  cache = new_result(@obj).to_cache
496
- cache.klass.should be(User)
506
+ cache.klass.should be(NilClass)
497
507
  end
498
508
  end
499
509
 
@@ -538,6 +548,19 @@ describe AridCache::CacheProxy::ResultProcessor do
538
548
  new_result(@obj).to_cache.should be_a(AridCache::CacheProxy::CachedResult)
539
549
  end
540
550
 
551
+ it "should not be able to infer the class of the results (without knowing the receiver class)" do
552
+ result = new_result(@ob)
553
+ result.options[:receiver_klass].should be_nil
554
+ cached = result.to_cache
555
+ cached.klass.should be(NilClass)
556
+ end
557
+
558
+ it "should return an empty array" do
559
+ result = new_result(@obj).to_result
560
+ result.should be_a(Array)
561
+ result.should be_empty
562
+ end
563
+
541
564
  it "should be a reflection" do
542
565
  new_result(@obj).is_activerecord_reflection?.should be_true
543
566
  end
@@ -572,4 +595,32 @@ describe AridCache::CacheProxy::ResultProcessor do
572
595
  @options[:result_klass].should be(@obj.class)
573
596
  end
574
597
  end
598
+
599
+ describe "cached result with NilClass" do
600
+ before :each do
601
+ @cached = AridCache::CacheProxy::CachedResult.new
602
+ @cached.klass = nil
603
+ end
604
+
605
+ it "should have NilClass" do
606
+ @cached.klass.should be(NilClass)
607
+ end
608
+
609
+ it "should return nil if no ids" do
610
+ with_deprecated_support {
611
+ @cached.has_ids?.should be_false
612
+ new_result(@cached).to_result.should be_nil
613
+ }
614
+ end
615
+
616
+ it "should return empty array" do
617
+ with_deprecated_support {
618
+ @cached.ids = []
619
+ @cached.has_ids?.should be_true
620
+ result = new_result(@cached).to_result
621
+ result.should be_a(Array)
622
+ result.should be_empty
623
+ }
624
+ end
625
+ end
575
626
  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: 1
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 4
9
- - 3
10
- version: 1.4.3
9
+ - 4
10
+ version: 1.4.4
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-21 00:00:00 -07:00
18
+ date: 2011-04-27 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency