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 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(name: 'John')
25
-
26
- user.attributes['_name_length']
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.attributes['_name_length']
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
@@ -11,4 +11,9 @@ RSpec::Core::RakeTask.new('spec:progress') do |spec|
11
11
  spec.pattern = "spec/**/*_spec.rb"
12
12
  end
13
13
 
14
+ desc "Open an irb session preloaded with this library"
15
+ task :console do
16
+ sh "irb -Ilib -rmongoid_cacheable"
17
+ end
18
+
14
19
  task :default => [:spec]
@@ -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
- alias_method uncached_name, name
18
- alias_method cached_name, field_name
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
- attributes[field_name]
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,3 +1,3 @@
1
1
  module MongoidCacheable
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,47 +1,60 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Mongoid::Cacheable do
3
+ class Book
4
+ include Mongoid::Document
5
+ include Mongoid::Cacheable
4
6
 
5
- let(:book) do
6
- Book.new(title: 'Life')
7
+ def say
8
+ true
7
9
  end
10
+ end
8
11
 
9
- let(:fields) do
10
- Book.fields
11
- end
12
+ describe Mongoid::Cacheable do
12
13
 
13
- it "does not persist prematurely" do
14
- book.title_length
15
- book.new_record?.should be_true
14
+ let(:book) { Book.new }
15
+
16
+ before :each do
17
+ book.cache_field(:_abc) { true }
16
18
  end
17
19
 
18
- it "adds a cached field to the document" do
19
- fields['_title_length'].should_not be_nil
20
+ it "caches a method's result" do
21
+ book.read_attribute(:_abc).should_not be_nil
20
22
  end
21
23
 
22
- context "when uncached" do
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
- it "caches method result" do
25
- book.title_length
26
- book.cached_title_length.should eq 4
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 previously cached" do
38
+ context "when a method is cached" do
32
39
 
33
- before do
34
- book._title_length = 17
40
+ before :each do
41
+ book.class.cache :say
42
+ book.cache_field(:_say) { 'message' }
35
43
  end
36
-
37
- it 'returns directly from cache' do
38
- book.title_length.should eq 17
44
+
45
+ it "creates a cached method alias" do
46
+ book.cached_say.should eq 'message'
39
47
  end
40
48
 
41
- it 'returns the uncached result' do
42
- book.uncached_title_length.should eq 4
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
@@ -10,8 +10,6 @@ Mongoid.configure do |config|
10
10
  config.connect_to database_id
11
11
  end
12
12
 
13
- Dir['./spec/models/*.rb'].each { |f| require f }
14
-
15
13
  RSpec.configure do |c|
16
14
  c.before(:each) do
17
15
  Mongoid.purge!
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.3
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-16 00:00:00.000000000 Z
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
@@ -1,11 +0,0 @@
1
- class Book
2
- include Mongoid::Document
3
- include Mongoid::Cacheable
4
-
5
- field :title
6
-
7
- def title_length
8
- title.length
9
- end
10
- cache :title_length, type: Integer
11
- end