mongoid_cacheable 0.0.2 → 0.0.3

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/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 characters_in_name
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[:characters_in_name]
26
+ user.attributes['_name_length']
27
27
  #=> nil
28
28
 
29
- user.characters_in_name
29
+ user.name_length
30
30
  #=> 4
31
31
 
32
- user.attributes[:characters_in_name]
32
+ user.attributes['_name_length']
33
33
  #=> 4
34
34
  ```
@@ -1,3 +1,3 @@
1
1
  module MongoidCacheable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -19,8 +19,8 @@ module Mongoid
19
19
 
20
20
  define_method(name) do
21
21
  unless attributes[field_name]
22
- send "#{field_name}=", send(uncached_name)
23
- save
22
+ # cache quitely with atomic set
23
+ set field_name, send(uncached_name)
24
24
  end
25
25
 
26
26
  attributes[field_name]
@@ -2,7 +2,22 @@ require 'spec_helper'
2
2
 
3
3
  describe Mongoid::Cacheable do
4
4
 
5
- let(:book) { Book.new(title: 'Life') }
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_cacheable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: