mongoid_cacheable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +34 -0
- data/Rakefile +14 -0
- data/lib/mongoid_cacheable/version.rb +3 -0
- data/lib/mongoid_cacheable.rb +24 -0
- data/mongoid_cacheable.gemspec +23 -0
- data/spec/models/book.rb +11 -0
- data/spec/mongoid_cacheable_spec.rb +52 -0
- data/spec/spec_helper.rb +24 -0
- metadata +124 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Noah H. Smith
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
mongoid_cacheable
|
2
|
+
=================
|
3
|
+
|
4
|
+
Cache Methods in any Mongoid Document.
|
5
|
+
|
6
|
+
## Getting Started
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
require 'mongoid'
|
10
|
+
require 'mongoid_cacheable'
|
11
|
+
|
12
|
+
class User
|
13
|
+
include Mongoid::Document
|
14
|
+
include Mongoid::Cacheable
|
15
|
+
|
16
|
+
field :name
|
17
|
+
cache :characters_in_name, type: Integer
|
18
|
+
|
19
|
+
def characters_in_name
|
20
|
+
name.length
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
user = User.new(name: 'John')
|
25
|
+
|
26
|
+
user.attributes[:characters_in_name]
|
27
|
+
#=> nil
|
28
|
+
|
29
|
+
user.characters_in_name
|
30
|
+
#=> 4
|
31
|
+
|
32
|
+
user.attributes[:characters_in_name]
|
33
|
+
#=> 4
|
34
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require "rspec/core/rake_task"
|
4
|
+
|
5
|
+
RSpec::Core::RakeTask.new("spec") do |spec|
|
6
|
+
spec.pattern = "spec/**/*_spec.rb"
|
7
|
+
end
|
8
|
+
|
9
|
+
RSpec::Core::RakeTask.new('spec:progress') do |spec|
|
10
|
+
spec.rspec_opts = %w(--format progress)
|
11
|
+
spec.pattern = "spec/**/*_spec.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
task :default => [:spec]
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "mongoid"
|
3
|
+
require "mongoid_cacheable/version"
|
4
|
+
|
5
|
+
module Mongoid
|
6
|
+
module Cacheable
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def cache( name, *options )
|
11
|
+
field_name = "_#{name}"
|
12
|
+
uncached_name = "uncached_#{name}"
|
13
|
+
cached_name = "cached_#{name}"
|
14
|
+
|
15
|
+
field field_name, *options
|
16
|
+
|
17
|
+
alias_method uncached_name, name
|
18
|
+
alias_method cached_name, field_name
|
19
|
+
|
20
|
+
define_method(name) { |*args| attributes[field_name] ||= send(uncached_name) }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/mongoid_cacheable/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Noah H. Smith"]
|
6
|
+
gem.email = ["nsmith@rendergardenmedia.com"]
|
7
|
+
gem.description = %q{Cache Methods in any Mongoid Document}
|
8
|
+
gem.summary = %q{Adds the ability to cache any instance method into a
|
9
|
+
Mongoid Document}
|
10
|
+
gem.homepage = "http://github.com/noazark/mongoid_cacheable"
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "mongoid_cacheable"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = MongoidCacheable::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency("mongoid", ["~> 3.0.0"])
|
19
|
+
gem.add_dependency("activesupport", ["~> 3.2.0"])
|
20
|
+
|
21
|
+
gem.add_development_dependency("rake")
|
22
|
+
gem.add_development_dependency("rspec")
|
23
|
+
end
|
data/spec/models/book.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::Cacheable do
|
4
|
+
|
5
|
+
let(:book) { Book.new(title: 'Life') }
|
6
|
+
|
7
|
+
context "when uncached" do
|
8
|
+
|
9
|
+
it "caches method result" do
|
10
|
+
book.title_length
|
11
|
+
book.cached_title_length.should eq 4
|
12
|
+
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
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when previously cached" do
|
25
|
+
|
26
|
+
before do
|
27
|
+
book._title_length = 17
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'returns directly from cache' do
|
31
|
+
book.title_length.should eq 17
|
32
|
+
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
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
|
3
|
+
require File.expand_path '../../lib/mongoid_cacheable', __FILE__
|
4
|
+
|
5
|
+
def database_id
|
6
|
+
ENV['CI'] ? "mongoid_cacheable_#{Process.pid}" : 'mongoid_cacheable_test'
|
7
|
+
end
|
8
|
+
|
9
|
+
Mongoid.configure do |config|
|
10
|
+
config.connect_to database_id
|
11
|
+
end
|
12
|
+
|
13
|
+
Dir['./spec/models/*.rb'].each { |f| require f }
|
14
|
+
|
15
|
+
RSpec.configure do |c|
|
16
|
+
c.before(:each) do
|
17
|
+
Mongoid.purge!
|
18
|
+
Mongoid::IdentityMap.clear
|
19
|
+
end
|
20
|
+
|
21
|
+
c.after(:suite) do
|
22
|
+
Mongoid::Threaded.sessions[:default].drop if ENV['CI']
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_cacheable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Noah H. Smith
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.2.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.2.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Cache Methods in any Mongoid Document
|
79
|
+
email:
|
80
|
+
- nsmith@rendergardenmedia.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .travis.yml
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- lib/mongoid_cacheable.rb
|
92
|
+
- lib/mongoid_cacheable/version.rb
|
93
|
+
- mongoid_cacheable.gemspec
|
94
|
+
- spec/models/book.rb
|
95
|
+
- spec/mongoid_cacheable_spec.rb
|
96
|
+
- spec/spec_helper.rb
|
97
|
+
homepage: http://github.com/noazark/mongoid_cacheable
|
98
|
+
licenses: []
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.8.24
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: Adds the ability to cache any instance method into a Mongoid Document
|
121
|
+
test_files:
|
122
|
+
- spec/models/book.rb
|
123
|
+
- spec/mongoid_cacheable_spec.rb
|
124
|
+
- spec/spec_helper.rb
|