prosto_cache 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/prosto_cache.rb +1 -1
- data/prosto_cache.gemspec +1 -1
- data/spec/prosto_cache_spec.rb +119 -102
- metadata +2 -2
data/lib/prosto_cache.rb
CHANGED
data/prosto_cache.gemspec
CHANGED
data/spec/prosto_cache_spec.rb
CHANGED
@@ -1,140 +1,157 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe "
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
describe "Model with prosto cache mixed in" do
|
4
|
+
let(:model_class) {
|
5
|
+
Class.new {
|
6
|
+
def self.after_save; end
|
7
|
+
include ProstoCache
|
8
|
+
}
|
9
|
+
}
|
10
10
|
|
11
11
|
describe '#cache' do
|
12
|
-
before do
|
13
|
-
class Foo
|
14
|
-
include ProstoCache
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
12
|
it "should add a cache method" do
|
19
|
-
|
20
|
-
|
13
|
+
model_class.cache.should_not be_nil
|
14
|
+
model_class.cache.should be_an_instance_of(ProstoCache::ProstoModelCache)
|
21
15
|
end
|
22
16
|
|
23
17
|
it "should not load cache if it was never accessed" do
|
24
|
-
|
25
|
-
|
18
|
+
model_class.cache.should_not_receive(:query_cache_signature)
|
19
|
+
model_class.should_not_receive(:all)
|
26
20
|
|
27
|
-
|
21
|
+
model_class.cache.should_not be_nil
|
28
22
|
end
|
29
23
|
end
|
30
24
|
|
31
|
-
|
32
|
-
|
33
|
-
|
25
|
+
describe 'cache' do
|
26
|
+
let(:model_class) {
|
27
|
+
Class.new {
|
28
|
+
def self.after_save; end
|
29
|
+
|
34
30
|
include ProstoCache
|
35
31
|
|
36
32
|
attr_accessor :name
|
37
33
|
def initialize(name)
|
38
34
|
self.name = name
|
39
35
|
end
|
36
|
+
}.tap { |model_class|
|
37
|
+
model_class.cache.stub(:query_cache_signature).once.and_return(:something)
|
38
|
+
model_class.stub(:all).once.and_return(%w(foo bar).map { |n| model_class.new(n) })
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
describe "#keys" do
|
43
|
+
it "should return all keys from the cache" do
|
44
|
+
model_class.cache.keys.should == [:foo, :bar]
|
40
45
|
end
|
41
|
-
|
42
|
-
Foo.cache.stub(:query_cache_signature).once.and_return(:foo)
|
43
|
-
Foo.stub(:all).once.and_return(%w(foo bar).map { |n| Foo.new(n) })
|
44
46
|
end
|
45
47
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
context 'when key is symbol' do
|
53
|
-
it "should raise an error for key that was not found" do
|
54
|
-
expect { Foo.cache[:nondef] }.to raise_error ProstoCache::BadCacheKeyError
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should return proper object for key that was found" do
|
58
|
-
Foo.cache[:foo].should_not be_nil
|
59
|
-
Foo.cache[:foo].name.should == 'foo'
|
48
|
+
describe "#values" do
|
49
|
+
it "should return all values from the cache" do
|
50
|
+
values = model_class.cache.values
|
51
|
+
values.should have(2).instances
|
52
|
+
values.map(&:name).should == %w(foo bar)
|
60
53
|
end
|
61
54
|
end
|
62
55
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
56
|
+
describe "#[]" do
|
57
|
+
context "with single key access" do
|
58
|
+
it "should load cache when it is accessed" do
|
59
|
+
model_class.should_receive(:all).once.and_return(%w(foo bar).map { |n| model_class.new(n) })
|
67
60
|
|
68
|
-
|
69
|
-
Foo.cache['foo'].should_not be_nil
|
70
|
-
Foo.cache['foo'].name.should == 'foo'
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
context "[] method with composite key access" do
|
76
|
-
before do
|
77
|
-
class Foo
|
78
|
-
include ProstoCache
|
79
|
-
cache_accessor_keys %w(key1 key2)
|
80
|
-
|
81
|
-
attr_accessor :name, :key1, :key2
|
82
|
-
def initialize(name, key1, key2)
|
83
|
-
self.name = name
|
84
|
-
self.key1 = key1
|
85
|
-
self.key2 = key2
|
61
|
+
model_class.cache[:foo]
|
86
62
|
end
|
87
|
-
end
|
88
63
|
|
89
|
-
|
90
|
-
|
91
|
-
|
64
|
+
context 'when key is symbol' do
|
65
|
+
it "should raise an error for key that was not found" do
|
66
|
+
expect { model_class.cache[:nondef] }.to raise_error ProstoCache::BadCacheKeyError
|
67
|
+
end
|
92
68
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
end
|
99
|
-
|
100
|
-
it "should raise an error when too many keys provided" do
|
101
|
-
expect { Foo.cache[:nondef1, :nondef2, :nondef3] }.to raise_error ProstoCache::BadCacheKeyError
|
102
|
-
expect { Foo.cache[:foo1, :foo2, :nondef] }.to raise_error ProstoCache::BadCacheKeyError
|
103
|
-
expect { Foo.cache['nondef1', 'nondef2', 'nondef3'] }.to raise_error ProstoCache::BadCacheKeyError
|
104
|
-
expect { Foo.cache['foo1', 'foo2', 'nondef'] }.to raise_error ProstoCache::BadCacheKeyError
|
105
|
-
end
|
69
|
+
it "should return proper object for key that was found" do
|
70
|
+
model_class.cache[:foo].should_not be_nil
|
71
|
+
model_class.cache[:foo].name.should == 'foo'
|
72
|
+
end
|
73
|
+
end
|
106
74
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
end
|
75
|
+
context 'when key is string' do
|
76
|
+
it "should return nil for key that was not found" do
|
77
|
+
model_class.cache['nondef'].should be_nil
|
78
|
+
end
|
112
79
|
|
113
|
-
|
114
|
-
|
115
|
-
|
80
|
+
it "should return proper object for key that was found" do
|
81
|
+
model_class.cache['foo'].should_not be_nil
|
82
|
+
model_class.cache['foo'].name.should == 'foo'
|
83
|
+
end
|
84
|
+
end
|
116
85
|
end
|
117
86
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
87
|
+
context "with composite key access" do
|
88
|
+
let(:model_class) {
|
89
|
+
Class.new {
|
90
|
+
def self.after_save; end
|
91
|
+
|
92
|
+
include ProstoCache
|
93
|
+
|
94
|
+
cache_accessor_keys %w(key1 key2)
|
95
|
+
|
96
|
+
attr_accessor :name, :key1, :key2
|
97
|
+
def initialize(name, key1, key2)
|
98
|
+
self.name = name
|
99
|
+
self.key1 = key1
|
100
|
+
self.key2 = key2
|
101
|
+
end
|
102
|
+
}.tap { |model_class|
|
103
|
+
model_class.cache.stub(:query_cache_signature).once.and_return(:foo)
|
104
|
+
model_class.stub(:all).once.and_return(%w(foo bar).map { |n| model_class.new(n, n + '1', n + '2') })
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
it "should raise an error when not enough keys provided" do
|
109
|
+
expect { model_class.cache[:nondef] }.to raise_error ProstoCache::BadCacheKeyError
|
110
|
+
expect { model_class.cache[:foo1] }.to raise_error ProstoCache::BadCacheKeyError
|
111
|
+
expect { model_class.cache['nondef'] }.to raise_error ProstoCache::BadCacheKeyError
|
112
|
+
expect { model_class.cache['foo1'] }.to raise_error ProstoCache::BadCacheKeyError
|
113
|
+
end
|
123
114
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
115
|
+
it "should raise an error when too many keys provided" do
|
116
|
+
expect { model_class.cache[:nondef1, :nondef2, :nondef3] }.to raise_error ProstoCache::BadCacheKeyError
|
117
|
+
expect { model_class.cache[:foo1, :foo2, :nondef] }.to raise_error ProstoCache::BadCacheKeyError
|
118
|
+
expect { model_class.cache['nondef1', 'nondef2', 'nondef3'] }.to raise_error ProstoCache::BadCacheKeyError
|
119
|
+
expect { model_class.cache['foo1', 'foo2', 'nondef'] }.to raise_error ProstoCache::BadCacheKeyError
|
120
|
+
end
|
129
121
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
122
|
+
context 'when last key is symbol' do
|
123
|
+
it "should raise an error for first key that was not found" do
|
124
|
+
expect { model_class.cache[:undef, :foo2] }.to raise_error ProstoCache::BadCacheKeyError
|
125
|
+
expect { model_class.cache['undef', :foo2] }.to raise_error ProstoCache::BadCacheKeyError
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should raise an error for last key that was not found" do
|
129
|
+
expect { model_class.cache[:foo1, :nondef] }.to raise_error ProstoCache::BadCacheKeyError
|
130
|
+
expect { model_class.cache['foo1', :nondef] }.to raise_error ProstoCache::BadCacheKeyError
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should return proper object for key that was found" do
|
134
|
+
model_class.cache[:foo1, :foo2].should_not be_nil
|
135
|
+
model_class.cache[:foo1, :foo2].name.should == 'foo'
|
136
|
+
end
|
137
|
+
end
|
134
138
|
|
135
|
-
|
136
|
-
|
137
|
-
|
139
|
+
context 'when last key is string' do
|
140
|
+
it "should return nil for first level key that was not found" do
|
141
|
+
model_class.cache['nondef', 'foo2'].should be_nil
|
142
|
+
model_class.cache[:nondef, 'foo2'].should be_nil
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should return nil for second level key that was not found" do
|
146
|
+
model_class.cache['foo1', 'nondef'].should be_nil
|
147
|
+
model_class.cache[:foo1, 'nondef'].should be_nil
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should return proper object for key that was found" do
|
151
|
+
model_class.cache['foo1', 'foo2'].should_not be_nil
|
152
|
+
model_class.cache['foo1', 'foo2'].name.should == 'foo'
|
153
|
+
end
|
154
|
+
end
|
138
155
|
end
|
139
156
|
end
|
140
157
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prosto_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -99,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
99
|
version: '0'
|
100
100
|
requirements: []
|
101
101
|
rubyforge_project:
|
102
|
-
rubygems_version: 1.8.
|
102
|
+
rubygems_version: 1.8.23
|
103
103
|
signing_key:
|
104
104
|
specification_version: 3
|
105
105
|
summary: Very simple caching for your ActiveRecord models.
|