kasket 0.8.2 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
data/kasket.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{kasket}
8
- s.version = "0.8.2"
8
+ s.version = "0.8.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mick Staugaard", "Eric Chapweske"]
12
- s.date = %q{2010-08-31}
12
+ s.date = %q{2010-09-02}
13
13
  s.description = %q{puts a cap on your queries}
14
14
  s.email = %q{mick@staugaard.com}
15
15
  s.extra_rdoc_files = [
@@ -11,10 +11,14 @@ module Kasket
11
11
  query = kasket_parser.parse(sanitize_sql(sql)) if use_kasket?
12
12
  if query && has_kasket_index_on?(query[:index])
13
13
  if query[:key].is_a?(Array)
14
- find_by_sql_with_kasket_on_id_array(sql, query)
14
+ find_by_sql_with_kasket_on_id_array(query[:key])
15
15
  else
16
16
  if value = Kasket.cache.read(query[:key])
17
- Array.wrap(value).collect { |record| instantiate(record.dup) }
17
+ if value.is_a?(Array)
18
+ find_by_sql_with_kasket_on_id_array(value)
19
+ else
20
+ Array.wrap(value).collect { |record| instantiate(record.dup) }
21
+ end
18
22
  else
19
23
  store_in_kasket(query[:key], find_by_sql_without_kasket(sql))
20
24
  end
@@ -24,11 +28,11 @@ module Kasket
24
28
  end
25
29
  end
26
30
 
27
- def find_by_sql_with_kasket_on_id_array(sql, query)
28
- key_value_map = Kasket.cache.read_multi(*query[:key])
31
+ def find_by_sql_with_kasket_on_id_array(keys)
32
+ key_value_map = Kasket.cache.read_multi(*keys)
29
33
  missing_ids = []
30
34
 
31
- query[:key].each do |key|
35
+ keys.each do |key|
32
36
  if value = key_value_map[key]
33
37
  key_value_map[key] = instantiate(value.dup)
34
38
  else
@@ -65,4 +69,3 @@ module Kasket
65
69
 
66
70
  end
67
71
  end
68
-
data/lib/kasket.rb CHANGED
@@ -14,7 +14,7 @@ module Kasket
14
14
  class Version
15
15
  MAJOR = 0
16
16
  MINOR = 8
17
- PATCH = 2
17
+ PATCH = 3
18
18
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
19
19
  end
20
20
 
@@ -4,31 +4,45 @@ class ReadMixinTest < ActiveSupport::TestCase
4
4
 
5
5
  context "find by sql with kasket" do
6
6
  setup do
7
- @database_results = [ { 'id' => 1, 'title' => 'Hello' } ]
8
- @records = @database_results.map { |r| Post.send(:instantiate, r) }
9
- Post.stubs(:find_by_sql_without_kasket).returns(@records)
7
+ @post_database_result = { 'id' => 1, 'title' => 'Hello' }
8
+ @post_records = [Post.send(:instantiate, @post_database_result)]
9
+ Post.stubs(:find_by_sql_without_kasket).returns(@post_records)
10
+
11
+ @comment_database_result = [{ 'id' => 1, 'body' => 'Hello' }, { 'id' => 2, 'body' => 'World' }]
12
+ @comment_records = @comment_database_result.map {|r| Comment.send(:instantiate, r)}
13
+ Comment.stubs(:find_by_sql_without_kasket).returns(@comment_records)
10
14
  end
11
15
 
12
16
  should "handle unsupported sql" do
13
17
  Kasket.cache.expects(:read).never
14
18
  Kasket.cache.expects(:write).never
15
- assert_equal @records, Post.find_by_sql_with_kasket('select unsupported sql statement')
19
+ assert_equal @post_records, Post.find_by_sql_with_kasket('select unsupported sql statement')
16
20
  end
17
21
 
18
22
  should "read results" do
19
- Kasket.cache.write("kasket-#{Kasket::Version::STRING}/posts/version=3558/id=1", @database_results.first)
20
- assert_equal [ @records.first ], Post.find_by_sql('SELECT * FROM `posts` WHERE (id = 1)')
23
+ Kasket.cache.write("kasket-#{Kasket::Version::STRING}/posts/version=3558/id=1", @post_database_result)
24
+ assert_equal @post_records, Post.find_by_sql('SELECT * FROM `posts` WHERE (id = 1)')
21
25
  end
22
26
 
23
27
  should "store results in kasket" do
24
28
  Post.find_by_sql('SELECT * FROM `posts` WHERE (id = 1)')
25
29
 
26
- assert_equal @database_results.first, Kasket.cache.read("kasket-#{Kasket::Version::STRING}/posts/version=3558/id=1")
30
+ assert_equal @post_database_result, Kasket.cache.read("kasket-#{Kasket::Version::STRING}/posts/version=3558/id=1")
31
+ end
32
+
33
+ should "store multiple records in cache" do
34
+ Comment.find_by_sql('SELECT * FROM `comments` WHERE (post_id = 1)')
35
+ stored_value = Kasket.cache.read("kasket-#{Kasket::Version::STRING}/comments/version=3476/post_id=1")
36
+ assert_equal(["kasket-#{Kasket::Version::STRING}/comments/version=3476/id=1", "kasket-#{Kasket::Version::STRING}/comments/version=3476/id=2"], stored_value)
37
+ assert_equal(@comment_database_result, stored_value.map {|key| Kasket.cache.read(key)})
38
+
39
+ Comment.expects(:find_by_sql_without_kasket).never
40
+ assert_equal(@comment_records, Comment.find_by_sql('SELECT * FROM `comments` WHERE (post_id = 1)'))
27
41
  end
28
42
 
29
43
  context "modifying results" do
30
44
  setup do
31
- Kasket.cache.write("kasket-#{Kasket::Version::STRING}/posts/version=3558/id=1", @database_results.first)
45
+ Kasket.cache.write("kasket-#{Kasket::Version::STRING}/posts/version=3558/id=1", @post_database_result)
32
46
  @record = Post.find_by_sql('SELECT * FROM `posts` WHERE (id = 1)').first
33
47
  @record.instance_variable_get(:@attributes)['id'] = 3
34
48
  end
data/test/test_models.rb CHANGED
@@ -10,6 +10,8 @@ create_model :comment do
10
10
  end
11
11
 
12
12
  belongs_to :post
13
+
14
+ has_kasket_on :post_id
13
15
  end
14
16
 
15
17
  create_model :post do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kasket
3
3
  version: !ruby/object:Gem::Version
4
- hash: 59
4
+ hash: 57
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 8
9
- - 2
10
- version: 0.8.2
9
+ - 3
10
+ version: 0.8.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mick Staugaard
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-08-31 00:00:00 -07:00
19
+ date: 2010-09-02 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency