kasket 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/kasket.rb CHANGED
@@ -3,6 +3,7 @@ require 'active_record'
3
3
  require 'active_support'
4
4
 
5
5
  require 'kasket/active_record_patches'
6
+ require 'kasket/version'
6
7
 
7
8
  module Kasket
8
9
  autoload :ConfigurationMixin, 'kasket/configuration_mixin'
@@ -11,13 +12,6 @@ module Kasket
11
12
 
12
13
  CONFIGURATION = {:max_collection_size => 100}
13
14
 
14
- class Version
15
- MAJOR = 1
16
- MINOR = 0
17
- PATCH = 3
18
- STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
19
- end
20
-
21
15
  module_function
22
16
 
23
17
  def setup(options = {})
@@ -41,14 +41,16 @@ module Kasket
41
41
  end
42
42
  end
43
43
 
44
- without_kasket do
45
- find(missing_ids).each do |instance|
46
- instance.store_in_kasket
47
- key_value_map[instance.kasket_key] = instance
44
+ if missing_ids.any?
45
+ without_kasket do
46
+ find_all_by_id(missing_ids).each do |instance|
47
+ instance.store_in_kasket
48
+ key_value_map[instance.kasket_key] = instance
49
+ end
48
50
  end
49
51
  end
50
52
 
51
- key_value_map.values
53
+ key_value_map.values.compact
52
54
  end
53
55
 
54
56
  protected
@@ -0,0 +1,8 @@
1
+ module Kasket
2
+ class Version
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ PATCH = 4
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -3,34 +3,53 @@ require File.expand_path("helper", File.dirname(__FILE__))
3
3
  class FindSomeTest < ActiveSupport::TestCase
4
4
  fixtures :blogs, :posts
5
5
 
6
- should "cache find(id, id) calls" do
7
- post1 = Post.first
8
- post2 = Post.last
6
+ def setup
7
+ @post1 = Post.first
8
+ @post2 = Post.last
9
+ Post.find(@post1.id, @post2.id)
10
+ assert Kasket.cache.read(@post1.kasket_key)
11
+ assert Kasket.cache.read(@post2.kasket_key)
12
+ end
9
13
 
10
- assert_nil(Kasket.cache.read(post1.kasket_key))
11
- assert_nil(Kasket.cache.read(post2.kasket_key))
14
+ should "use cache for find(id, id) calls" do
15
+ Post.connection.expects(:select_all).never
16
+ Post.find(@post1.id, @post2.id)
17
+ end
12
18
 
13
- Post.find(post1.id, post2.id)
19
+ should "cache when found using find(id, id) calls" do
20
+ Kasket.cache.delete(@post1.kasket_key)
21
+ Kasket.cache.delete(@post2.kasket_key)
14
22
 
15
- assert(Kasket.cache.read(post1.kasket_key))
16
- assert(Kasket.cache.read(post2.kasket_key))
17
- Post.connection.expects(:select_all).never
18
- Post.find(post1.id, post2.id)
23
+ Post.find(@post1.id, @post2.id)
24
+
25
+ assert Kasket.cache.read(@post1.kasket_key)
26
+ assert Kasket.cache.read(@post2.kasket_key)
19
27
  end
20
28
 
21
29
  should "only lookup the records that are not in the cache" do
22
- post1 = Post.first
23
- post2 = Post.last
24
- assert_equal(post1, Post.find(post1.id))
25
- assert(Kasket.cache.read(post1.kasket_key))
26
- assert_nil(Kasket.cache.read(post2.kasket_key))
30
+ Kasket.cache.delete(@post2.kasket_key)
27
31
 
28
- Post.expects(:find_by_sql_without_kasket).with("SELECT * FROM \"posts\" WHERE (\"posts\".\"id\" = #{post2.id}) ").returns([post2])
29
- found_posts = Post.find(post1.id, post2.id)
30
- assert_equal([post1, post2].map(&:id).sort, found_posts.map(&:id).sort)
32
+ # has to lookup post2 via db
33
+ Post.expects(:find_by_sql_without_kasket).returns([@post2])
34
+ found_posts = Post.find(@post1.id, @post2.id)
35
+ assert_equal [@post1, @post2].map(&:id).sort, found_posts.map(&:id).sort
31
36
 
37
+ # now all are cached
32
38
  Post.expects(:find_by_sql_without_kasket).never
33
- found_posts = Post.find(post1.id, post2.id)
34
- assert_equal([post1, post2].map(&:id).sort, found_posts.map(&:id).sort)
39
+ found_posts = Post.find(@post1.id, @post2.id)
40
+ assert_equal [@post1, @post2].map(&:id).sort, found_posts.map(&:id).sort
41
+ end
42
+
43
+ context "unfound" do
44
+ should "ignore unfound when using find_all_by_id" do
45
+ found_posts = Post.find_all_by_id([@post1.id, 1231232])
46
+ assert_equal [@post1.id], found_posts.map(&:id)
47
+ end
48
+
49
+ should "not ignore unfound when using find" do
50
+ assert_raise ActiveRecord::RecordNotFound do
51
+ Post.find(@post1.id, 1231232)
52
+ end
53
+ end
35
54
  end
36
55
  end
data/test/helper.rb CHANGED
@@ -18,6 +18,8 @@ require 'kasket'
18
18
 
19
19
  Kasket.setup
20
20
 
21
+ require 'shoulda'
22
+
21
23
  class ActiveSupport::TestCase
22
24
  include ActiveRecord::TestFixtures
23
25
 
@@ -58,4 +60,4 @@ module Rails
58
60
  end
59
61
  end
60
62
 
61
- require 'test_models'
63
+ require 'test_models'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kasket
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-06-15 00:00:00.000000000 Z
13
+ date: 2012-11-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -138,6 +138,7 @@ files:
138
138
  - lib/kasket/query_parser.rb
139
139
  - lib/kasket/read_mixin.rb
140
140
  - lib/kasket/reload_association_mixin.rb
141
+ - lib/kasket/version.rb
141
142
  - lib/kasket/write_mixin.rb
142
143
  - lib/kasket.rb
143
144
  - README.rdoc
@@ -168,6 +169,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
168
169
  - - ! '>='
169
170
  - !ruby/object:Gem::Version
170
171
  version: '0'
172
+ segments:
173
+ - 0
174
+ hash: 4530626145164262218
171
175
  required_rubygems_version: !ruby/object:Gem::Requirement
172
176
  none: false
173
177
  requirements: