mongoid_cacheable 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -0
- data/README.md +12 -8
- data/Rakefile +5 -0
- data/lib/mongoid_cacheable.rb +19 -10
- data/lib/mongoid_cacheable/version.rb +1 -1
- data/spec/mongoid_cacheable_spec.rb +38 -25
- data/spec/spec_helper.rb +0 -2
- metadata +3 -4
- data/spec/models/book.rb +0 -11
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3@mongoid_cacheable
|
data/README.md
CHANGED
@@ -3,6 +3,9 @@ mongoid_cacheable
|
|
3
3
|
|
4
4
|
Cache Methods in any Mongoid Document.
|
5
5
|
|
6
|
+
[![Build Status](https://secure.travis-ci.org/noazark/mongoid_cacheable.png?branch=master)](http://travis-ci.org/noazark/mongoid_cacheable)
|
7
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/noazark/mongoid_cacheable)
|
8
|
+
|
6
9
|
## Getting Started
|
7
10
|
|
8
11
|
```ruby
|
@@ -21,14 +24,15 @@ class User
|
|
21
24
|
cache :name_length, type: Integer
|
22
25
|
end
|
23
26
|
|
24
|
-
user = User.new
|
25
|
-
|
26
|
-
user.
|
27
|
-
#=> nil
|
28
|
-
|
29
|
-
user.name_length
|
27
|
+
user = User.new
|
28
|
+
user.name = 'John'
|
29
|
+
user.cached_name_length # calling the cached method for the first time
|
30
30
|
#=> 4
|
31
|
-
|
32
|
-
user.
|
31
|
+
user.name = 'Ron Jon'
|
32
|
+
user.cached_name_length # the previous result is still cached
|
33
|
+
#=> 4
|
34
|
+
user.name_length # access the original method
|
35
|
+
#=> 7
|
36
|
+
user.read_attribute :_name_length # the raw cached field is unchanged
|
33
37
|
#=> 4
|
34
38
|
```
|
data/Rakefile
CHANGED
data/lib/mongoid_cacheable.rb
CHANGED
@@ -9,23 +9,32 @@ module Mongoid
|
|
9
9
|
module ClassMethods
|
10
10
|
def cache( name, *options )
|
11
11
|
field_name = "_#{name}"
|
12
|
-
uncached_name = "uncached_#{name}"
|
13
12
|
cached_name = "cached_#{name}"
|
13
|
+
clear_cached_name = "clear_cached_#{name}"
|
14
14
|
|
15
15
|
field field_name, *options
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
define_method(name) do
|
21
|
-
unless attributes[field_name]
|
22
|
-
# cache quitely with atomic set
|
23
|
-
set field_name, send(uncached_name)
|
24
|
-
end
|
17
|
+
define_method(cached_name) do
|
18
|
+
cache_field(field_name, &method(name))
|
19
|
+
end
|
25
20
|
|
26
|
-
|
21
|
+
define_method(clear_cached_name) do
|
22
|
+
clear_cache_field(field_name)
|
27
23
|
end
|
28
24
|
end
|
29
25
|
end
|
26
|
+
|
27
|
+
def cache_field(field_name, &block)
|
28
|
+
unless read_attribute(field_name)
|
29
|
+
# cache quitely with atomic set
|
30
|
+
set field_name, yield
|
31
|
+
end
|
32
|
+
|
33
|
+
read_attribute(field_name)
|
34
|
+
end
|
35
|
+
|
36
|
+
def clear_cache_field(field_name)
|
37
|
+
unset field_name
|
38
|
+
end
|
30
39
|
end
|
31
40
|
end
|
@@ -1,47 +1,60 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
3
|
+
class Book
|
4
|
+
include Mongoid::Document
|
5
|
+
include Mongoid::Cacheable
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
+
def say
|
8
|
+
true
|
7
9
|
end
|
10
|
+
end
|
8
11
|
|
9
|
-
|
10
|
-
Book.fields
|
11
|
-
end
|
12
|
+
describe Mongoid::Cacheable do
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
let(:book) { Book.new }
|
15
|
+
|
16
|
+
before :each do
|
17
|
+
book.cache_field(:_abc) { true }
|
16
18
|
end
|
17
19
|
|
18
|
-
it "
|
19
|
-
|
20
|
+
it "caches a method's result" do
|
21
|
+
book.read_attribute(:_abc).should_not be_nil
|
20
22
|
end
|
21
23
|
|
22
|
-
|
24
|
+
it "clears a cache result" do
|
25
|
+
book.clear_cache_field(:_abc)
|
26
|
+
book.read_attribute(:_abc).should be_nil
|
27
|
+
end
|
23
28
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
29
|
+
it "caches wihtout saving the parent" do
|
30
|
+
book.new_record?.should be_true
|
31
|
+
end
|
28
32
|
|
33
|
+
it "cache is persistant" do
|
34
|
+
book.save
|
35
|
+
book.reload.cache_field(:_abc).should_not be_nil
|
29
36
|
end
|
30
37
|
|
31
|
-
context "when
|
38
|
+
context "when a method is cached" do
|
32
39
|
|
33
|
-
before do
|
34
|
-
book.
|
40
|
+
before :each do
|
41
|
+
book.class.cache :say
|
42
|
+
book.cache_field(:_say) { 'message' }
|
35
43
|
end
|
36
|
-
|
37
|
-
it
|
38
|
-
book.
|
44
|
+
|
45
|
+
it "creates a cached method alias" do
|
46
|
+
book.cached_say.should eq 'message'
|
39
47
|
end
|
40
48
|
|
41
|
-
it
|
42
|
-
book.
|
49
|
+
it "original method remains uncached" do
|
50
|
+
book.say.should eq true
|
43
51
|
end
|
44
|
-
|
52
|
+
|
53
|
+
it "creates a clear cached method alias" do
|
54
|
+
book.clear_cached_say
|
55
|
+
book.cached_say.should eq true
|
56
|
+
end
|
57
|
+
|
45
58
|
end
|
46
59
|
|
47
60
|
end
|
data/spec/spec_helper.rb
CHANGED
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
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
@@ -83,6 +83,7 @@ extensions: []
|
|
83
83
|
extra_rdoc_files: []
|
84
84
|
files:
|
85
85
|
- .gitignore
|
86
|
+
- .rvmrc
|
86
87
|
- .travis.yml
|
87
88
|
- Gemfile
|
88
89
|
- LICENSE
|
@@ -91,7 +92,6 @@ files:
|
|
91
92
|
- lib/mongoid_cacheable.rb
|
92
93
|
- lib/mongoid_cacheable/version.rb
|
93
94
|
- mongoid_cacheable.gemspec
|
94
|
-
- spec/models/book.rb
|
95
95
|
- spec/mongoid_cacheable_spec.rb
|
96
96
|
- spec/spec_helper.rb
|
97
97
|
homepage: http://github.com/noazark/mongoid_cacheable
|
@@ -119,6 +119,5 @@ signing_key:
|
|
119
119
|
specification_version: 3
|
120
120
|
summary: Adds the ability to cache any instance method into a Mongoid Document
|
121
121
|
test_files:
|
122
|
-
- spec/models/book.rb
|
123
122
|
- spec/mongoid_cacheable_spec.rb
|
124
123
|
- spec/spec_helper.rb
|