method_caching 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +63 -0
- data/LICENSE.txt +20 -0
- data/README.md +51 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/method_caching.rb +48 -0
- data/spec/method_caching_spec.rb +82 -0
- data/spec/spec_helper.rb +12 -0
- metadata +126 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec"
|
10
|
+
gem "rdoc"
|
11
|
+
gem "bundler"
|
12
|
+
gem "jeweler", "~> 1.8.7"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.3.5)
|
5
|
+
builder (3.2.2)
|
6
|
+
diff-lcs (1.2.4)
|
7
|
+
faraday (0.8.8)
|
8
|
+
multipart-post (~> 1.2.0)
|
9
|
+
git (1.2.6)
|
10
|
+
github_api (0.10.1)
|
11
|
+
addressable
|
12
|
+
faraday (~> 0.8.1)
|
13
|
+
hashie (>= 1.2)
|
14
|
+
multi_json (~> 1.4)
|
15
|
+
nokogiri (~> 1.5.2)
|
16
|
+
oauth2
|
17
|
+
hashie (2.0.5)
|
18
|
+
highline (1.6.20)
|
19
|
+
httpauth (0.2.0)
|
20
|
+
jeweler (1.8.8)
|
21
|
+
builder
|
22
|
+
bundler (~> 1.0)
|
23
|
+
git (>= 1.2.5)
|
24
|
+
github_api (= 0.10.1)
|
25
|
+
highline (>= 1.6.15)
|
26
|
+
nokogiri (= 1.5.10)
|
27
|
+
rake
|
28
|
+
rdoc
|
29
|
+
json (1.8.1)
|
30
|
+
jwt (0.1.8)
|
31
|
+
multi_json (>= 1.5)
|
32
|
+
multi_json (1.8.2)
|
33
|
+
multi_xml (0.5.5)
|
34
|
+
multipart-post (1.2.0)
|
35
|
+
nokogiri (1.5.10)
|
36
|
+
oauth2 (0.9.2)
|
37
|
+
faraday (~> 0.8)
|
38
|
+
httpauth (~> 0.2)
|
39
|
+
jwt (~> 0.1.4)
|
40
|
+
multi_json (~> 1.0)
|
41
|
+
multi_xml (~> 0.5)
|
42
|
+
rack (~> 1.2)
|
43
|
+
rack (1.5.2)
|
44
|
+
rake (10.1.0)
|
45
|
+
rdoc (4.0.1)
|
46
|
+
json (~> 1.4)
|
47
|
+
rspec (2.14.1)
|
48
|
+
rspec-core (~> 2.14.0)
|
49
|
+
rspec-expectations (~> 2.14.0)
|
50
|
+
rspec-mocks (~> 2.14.0)
|
51
|
+
rspec-core (2.14.6)
|
52
|
+
rspec-expectations (2.14.3)
|
53
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
54
|
+
rspec-mocks (2.14.4)
|
55
|
+
|
56
|
+
PLATFORMS
|
57
|
+
ruby
|
58
|
+
|
59
|
+
DEPENDENCIES
|
60
|
+
bundler
|
61
|
+
jeweler (~> 1.8.7)
|
62
|
+
rdoc
|
63
|
+
rspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 HuyHa
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# method_caching
|
2
|
+
|
3
|
+
method_caching is a simple Ruby library for caching instance method of a class (using Rails.cache as cache store)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```bash
|
8
|
+
gem install method_caching
|
9
|
+
```
|
10
|
+
|
11
|
+
or add to Gemfile
|
12
|
+
|
13
|
+
```bash
|
14
|
+
gem "method_caching"
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class User < ActiveRecord::Base
|
21
|
+
method_caching
|
22
|
+
|
23
|
+
def full_name
|
24
|
+
"#{first_name} #{last_name}"
|
25
|
+
end
|
26
|
+
cache_method :full_name
|
27
|
+
cache_method_clear_on :save, :full_name # Clear cache for method full_name whenever method 'save' is called
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
method_caching will use the record's id as the default identifier to generate the cache key. You can change the default identifier by passing custom identifier
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class User < ActiveRecord::Base
|
35
|
+
method_caching :custom_identifier
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
## Contributing to method_caching
|
40
|
+
|
41
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
42
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
43
|
+
* Fork the project.
|
44
|
+
* Start a feature/bugfix branch.
|
45
|
+
* Commit and push until you are happy with your contribution.
|
46
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
47
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
48
|
+
|
49
|
+
## Copyright
|
50
|
+
|
51
|
+
Copyright (c) 2013 Huy Ha. See LICENSE.txt for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "method_caching"
|
18
|
+
gem.homepage = "http://github.com/huyha85/method_caching"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Simple Ruby library for caching instance method of a class}
|
21
|
+
gem.description = %Q{Simple Ruby library for caching instance method of a class. Using Rails.cache as default caching.}
|
22
|
+
gem.email = "huy.ha@eastagile.com"
|
23
|
+
gem.authors = ["HuyHa"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "method_caching #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module MethodCaching
|
2
|
+
module ClassMethods
|
3
|
+
def method_caching(identifier_attr = :id)
|
4
|
+
@identifier = identifier_attr.to_sym
|
5
|
+
self.send(:include, InstanceMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
def cache_method(method)
|
9
|
+
old = "_#{method}".to_sym
|
10
|
+
alias_method old, method
|
11
|
+
define_method method do |*args|
|
12
|
+
Rails.cache.fetch(cache_key_name(method)) do
|
13
|
+
send(old, *args)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def cache_method_clear_on(clear_method, cache_method)
|
19
|
+
original_method = "_cache_method_clear_on_#{clear_method}"
|
20
|
+
alias_method original_method, clear_method
|
21
|
+
|
22
|
+
define_method clear_method do |*args, &blk|
|
23
|
+
self.clear_cache_on cache_method
|
24
|
+
send(original_method, *args, &blk)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def identifier
|
29
|
+
@identifier
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module InstanceMethods
|
34
|
+
def clear_cache_on(method)
|
35
|
+
Rails.cache.delete(cache_key_name(method))
|
36
|
+
end
|
37
|
+
|
38
|
+
def cache_key_name(method)
|
39
|
+
identifier_method = self.class.send(:identifier)
|
40
|
+
"#{self.class.to_s}_#{self.send(identifier_method)}_#{method.to_s}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
if defined?(ActiveRecord)
|
46
|
+
ActiveRecord::Base.send(:include, MethodCaching::InstanceMethods)
|
47
|
+
ActiveRecord::Base.send(:extend, MethodCaching::ClassMethods)
|
48
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class TestClass
|
4
|
+
extend MethodCaching::ClassMethods
|
5
|
+
include MethodCaching::InstanceMethods
|
6
|
+
method_caching :identifier
|
7
|
+
|
8
|
+
def method_to_be_cleared; end
|
9
|
+
|
10
|
+
def method_to_be_cached
|
11
|
+
calculate(2)
|
12
|
+
end
|
13
|
+
cache_method :method_to_be_cached
|
14
|
+
cache_method_clear_on :method_to_be_cleared, :method_to_be_cached
|
15
|
+
|
16
|
+
def identifier
|
17
|
+
"id"
|
18
|
+
end
|
19
|
+
|
20
|
+
def calculate(x)
|
21
|
+
x
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Rails
|
26
|
+
def self.cache
|
27
|
+
RailsCache.new
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class RailsCache
|
32
|
+
@@cache_hash = {}
|
33
|
+
def fetch(key, &block)
|
34
|
+
if @@cache_hash[key]
|
35
|
+
@@cache_hash[key]
|
36
|
+
else
|
37
|
+
@@cache_hash[key] = *block.call
|
38
|
+
@@cache_hash[key]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def delete(key)
|
43
|
+
@@cache_hash[key] = nil
|
44
|
+
end
|
45
|
+
|
46
|
+
def clear
|
47
|
+
@@cache_hash = {}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "MethodCaching" do
|
52
|
+
let(:test_class) { TestClass.new }
|
53
|
+
before do
|
54
|
+
Rails.cache.clear
|
55
|
+
end
|
56
|
+
describe "#cache_method" do
|
57
|
+
context "call cached method for multiple times" do
|
58
|
+
it "should actually call the method once" do
|
59
|
+
test_class.should_receive(:calculate).once
|
60
|
+
2.times { test_class.method_to_be_cached }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#clear_cache_on" do
|
66
|
+
it "should clear cache for method on object" do
|
67
|
+
test_class.should_receive(:calculate).twice
|
68
|
+
test_class.method_to_be_cached
|
69
|
+
test_class.clear_cache_on :method_to_be_cached
|
70
|
+
test_class.method_to_be_cached
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#cache_method_clear_on" do
|
75
|
+
it "should clear cache when calling clear_method" do
|
76
|
+
test_class.should_receive(:calculate).twice
|
77
|
+
test_class.method_to_be_cached
|
78
|
+
test_class.method_to_be_cleared
|
79
|
+
test_class.method_to_be_cached
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'method_caching'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: method_caching
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- HuyHa
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rdoc
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
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: jeweler
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.8.7
|
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: 1.8.7
|
78
|
+
description: Simple Ruby library for caching instance method of a class. Using Rails.cache
|
79
|
+
as default caching.
|
80
|
+
email: huy.ha@eastagile.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files:
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.md
|
86
|
+
files:
|
87
|
+
- .document
|
88
|
+
- .rspec
|
89
|
+
- Gemfile
|
90
|
+
- Gemfile.lock
|
91
|
+
- LICENSE.txt
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- VERSION
|
95
|
+
- lib/method_caching.rb
|
96
|
+
- spec/method_caching_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
homepage: http://github.com/huyha85/method_caching
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
hash: 3994657104755449766
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.8.25
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: Simple Ruby library for caching instance method of a class
|
126
|
+
test_files: []
|