cache_helper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cache_helper.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = CacheHelper
2
+
3
+ Overrides ActiveRecord::Base#cache_key to return unique keys for new records
4
+ and adds methods to more easily work with Rails caching.
5
+
6
+ Author:: Clyde Law (mailto:clyde@futureadvisor.com)
7
+ License:: Released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cache_helper/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cache_helper"
7
+ s.version = CacheHelper::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Clyde Law"]
10
+ s.email = ["clyde@futureadvisor.com"]
11
+ s.homepage = %q{http://github.com/FutureAdvisor/cache_helper}
12
+ s.summary = %q{Adds methods to more easily work with Rails caching.}
13
+ s.description = %q{Overrides ActiveRecord::Base#cache_key to return unique keys for new records and adds methods to more easily work with Rails caching.}
14
+ s.license = 'MIT'
15
+
16
+ s.add_dependency('activerecord', '>= 2.1.0')
17
+ s.add_dependency('alias_helper')
18
+
19
+ s.rubyforge_project = "cache_helper"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,48 @@
1
+ module CacheHelper
2
+ def self.included(base)
3
+ base.alias_method_chain :cache_key, :new_record_id
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ # Overrides ActiveRecord::Base#cache_key to return unique keys for new
8
+ # records.
9
+ def cache_key_with_new_record_id
10
+ key = cache_key_without_new_record_id
11
+ if self.new_record?
12
+ # The object is not yet database-backed; append the object's ID and the
13
+ # current time to use as a provisional cache key.
14
+ if @temp_cache_key.nil?
15
+ @temp_cache_key = key << "(#{self.__id__})-#{Time.now.to_s(:number)}"
16
+ else
17
+ key = @temp_cache_key
18
+ end
19
+ end
20
+ key
21
+ end
22
+
23
+ # Returns a cache key associated with the record for the specified method
24
+ # and options.
25
+ def method_cache_key(method, options = {})
26
+ key = self.cache_key + "-#{method.to_s}"
27
+ key << ".#{options.hash.to_s(36)}" unless options.empty?
28
+ key
29
+ end
30
+
31
+ module ClassMethods
32
+ private
33
+ # Wraps an instance method with basic caching functionality.
34
+ def cache_method(method)
35
+ method_with_caching, method_without_caching = method_chain_aliases(method, :caching)
36
+ define_method(method_with_caching.to_sym) {
37
+ Rails.cache.fetch(method_cache_key(method)) do
38
+ self.__send__(method_without_caching.to_sym)
39
+ end
40
+ }
41
+ alias_method_chain method, :caching
42
+ end
43
+ end
44
+ end
45
+
46
+ class ActiveRecord::Base
47
+ include CacheHelper
48
+ end
@@ -0,0 +1,3 @@
1
+ module CacheHelper
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache_helper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Clyde Law
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-20 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 11
29
+ segments:
30
+ - 2
31
+ - 1
32
+ - 0
33
+ version: 2.1.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: alias_helper
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: Overrides ActiveRecord::Base#cache_key to return unique keys for new records and adds methods to more easily work with Rails caching.
51
+ email:
52
+ - clyde@futureadvisor.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - .gitignore
61
+ - Gemfile
62
+ - README.rdoc
63
+ - Rakefile
64
+ - cache_helper.gemspec
65
+ - lib/cache_helper.rb
66
+ - lib/cache_helper/version.rb
67
+ homepage: http://github.com/FutureAdvisor/cache_helper
68
+ licenses:
69
+ - MIT
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project: cache_helper
96
+ rubygems_version: 1.7.2
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Adds methods to more easily work with Rails caching.
100
+ test_files: []
101
+