mongoid_cacheable 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -5
- data/lib/mongoid_cacheable/version.rb +1 -1
- data/lib/mongoid_cacheable.rb +2 -2
- data/spec/mongoid_cacheable_spec.rb +18 -23
- metadata +1 -1
data/README.md
CHANGED
@@ -14,21 +14,21 @@ class User
|
|
14
14
|
include Mongoid::Cacheable
|
15
15
|
|
16
16
|
field :name
|
17
|
-
cache :characters_in_name, type: Integer
|
18
17
|
|
19
|
-
def
|
18
|
+
def name_length
|
20
19
|
name.length
|
21
20
|
end
|
21
|
+
cache :name_length, type: Integer
|
22
22
|
end
|
23
23
|
|
24
24
|
user = User.new(name: 'John')
|
25
25
|
|
26
|
-
user.attributes[
|
26
|
+
user.attributes['_name_length']
|
27
27
|
#=> nil
|
28
28
|
|
29
|
-
user.
|
29
|
+
user.name_length
|
30
30
|
#=> 4
|
31
31
|
|
32
|
-
user.attributes[
|
32
|
+
user.attributes['_name_length']
|
33
33
|
#=> 4
|
34
34
|
```
|
data/lib/mongoid_cacheable.rb
CHANGED
@@ -2,7 +2,22 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Mongoid::Cacheable do
|
4
4
|
|
5
|
-
let(:book)
|
5
|
+
let(:book) do
|
6
|
+
Book.new(title: 'Life')
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:fields) do
|
10
|
+
Book.fields
|
11
|
+
end
|
12
|
+
|
13
|
+
it "does not persist prematurely" do
|
14
|
+
book.title_length
|
15
|
+
book.new_record?.should be_true
|
16
|
+
end
|
17
|
+
|
18
|
+
it "adds a cached field to the document" do
|
19
|
+
fields['_title_length'].should_not be_nil
|
20
|
+
end
|
6
21
|
|
7
22
|
context "when uncached" do
|
8
23
|
|
@@ -10,14 +25,6 @@ describe Mongoid::Cacheable do
|
|
10
25
|
book.title_length
|
11
26
|
book.cached_title_length.should eq 4
|
12
27
|
end
|
13
|
-
|
14
|
-
describe '#cached' do
|
15
|
-
|
16
|
-
it 'returns directly from cache' do
|
17
|
-
book.cached_title_length.should eq nil
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
28
|
|
22
29
|
end
|
23
30
|
|
@@ -30,21 +37,9 @@ describe Mongoid::Cacheable do
|
|
30
37
|
it 'returns directly from cache' do
|
31
38
|
book.title_length.should eq 17
|
32
39
|
end
|
33
|
-
|
34
|
-
describe '#cached' do
|
35
|
-
|
36
|
-
it 'returns directly from cache' do
|
37
|
-
book.cached_title_length.should eq 17
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
describe '#uncached' do
|
43
|
-
|
44
|
-
it 'ignore cached value and return caclulated value' do
|
45
|
-
book.uncached_title_length.should eq 4
|
46
|
-
end
|
47
40
|
|
41
|
+
it 'returns the uncached result' do
|
42
|
+
book.uncached_title_length.should eq 4
|
48
43
|
end
|
49
44
|
|
50
45
|
end
|