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.
@@ -65,7 +65,7 @@ module ProstoCache
65
65
  hash[key.to_sym] = value
66
66
  end
67
67
 
68
- def_delegators :hash, :to_s, :inspect
68
+ def_delegators :hash, :to_s, :inspect, :keys, :values
69
69
 
70
70
  private
71
71
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "prosto_cache"
5
- s.version = "0.2.0"
5
+ s.version = "0.2.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Olek Poplavsky"]
@@ -1,140 +1,157 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- describe "ProstoCache" do
4
- before do
5
- Object.send(:remove_const, 'Foo') if Object.const_defined? 'Foo'
6
-
7
- class Foo; end
8
- Foo.stub(:after_save)
9
- end
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
- Foo.cache.should_not be_nil
20
- Foo.cache.should be_an_instance_of(ProstoCache::ProstoModelCache)
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
- Foo.cache.should_not_receive(:query_cache_signature)
25
- Foo.should_not_receive(:all)
18
+ model_class.cache.should_not_receive(:query_cache_signature)
19
+ model_class.should_not_receive(:all)
26
20
 
27
- Foo.cache.should_not be_nil
21
+ model_class.cache.should_not be_nil
28
22
  end
29
23
  end
30
24
 
31
- context "[] method with single key access" do
32
- before do
33
- class Foo
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
- it "should load cache when it is accessed" do
47
- Foo.should_receive(:all).once.and_return(%w(foo bar).map { |n| Foo.new(n) })
48
-
49
- Foo.cache[:foo]
50
- end
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
- context 'when key is string' do
64
- it "should return nil for key that was not found" do
65
- Foo.cache['nondef'].should be_nil
66
- end
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
- it "should return proper object for key that was found" do
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
- Foo.cache.stub(:query_cache_signature).once.and_return(:foo)
90
- Foo.stub(:all).once.and_return(%w(foo bar).map { |n| Foo.new(n, n + '1', n + '2') })
91
- end
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
- it "should raise an error when not enough keys provided" do
94
- expect { Foo.cache[:nondef] }.to raise_error ProstoCache::BadCacheKeyError
95
- expect { Foo.cache[:foo1] }.to raise_error ProstoCache::BadCacheKeyError
96
- expect { Foo.cache['nondef'] }.to raise_error ProstoCache::BadCacheKeyError
97
- expect { Foo.cache['foo1'] }.to raise_error ProstoCache::BadCacheKeyError
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
- context 'when last key is symbol' do
108
- it "should raise an error for first key that was not found" do
109
- expect { Foo.cache[:undef, :foo2] }.to raise_error ProstoCache::BadCacheKeyError
110
- expect { Foo.cache['undef', :foo2] }.to raise_error ProstoCache::BadCacheKeyError
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
- it "should raise an error for last key that was not found" do
114
- expect { Foo.cache[:foo1, :nondef] }.to raise_error ProstoCache::BadCacheKeyError
115
- expect { Foo.cache['foo1', :nondef] }.to raise_error ProstoCache::BadCacheKeyError
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
- it "should return proper object for key that was found" do
119
- Foo.cache[:foo1, :foo2].should_not be_nil
120
- Foo.cache[:foo1, :foo2].name.should == 'foo'
121
- end
122
- end
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
- context 'when last key is string' do
125
- it "should return nil for first level key that was not found" do
126
- Foo.cache['nondef', 'foo2'].should be_nil
127
- Foo.cache[:nondef, 'foo2'].should be_nil
128
- end
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
- it "should return nil for second level key that was not found" do
131
- Foo.cache['foo1', 'nondef'].should be_nil
132
- Foo.cache[:foo1, 'nondef'].should be_nil
133
- end
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
- it "should return proper object for key that was found" do
136
- Foo.cache['foo1', 'foo2'].should_not be_nil
137
- Foo.cache['foo1', 'foo2'].name.should == 'foo'
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.0
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.24
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.