pack_rat 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,19 @@
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 "rdoc"
10
+ gem "bundler"
11
+ gem "jeweler"
12
+ gem 'simplecov'
13
+ end
14
+ group :test do
15
+ gem 'rspec'
16
+ gem 'factory_girl'
17
+ end
18
+
19
+ gem 'rails'
data/Gemfile.lock ADDED
@@ -0,0 +1,113 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionmailer (3.2.11)
5
+ actionpack (= 3.2.11)
6
+ mail (~> 2.4.4)
7
+ actionpack (3.2.11)
8
+ activemodel (= 3.2.11)
9
+ activesupport (= 3.2.11)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ journey (~> 1.0.4)
13
+ rack (~> 1.4.0)
14
+ rack-cache (~> 1.2)
15
+ rack-test (~> 0.6.1)
16
+ sprockets (~> 2.2.1)
17
+ activemodel (3.2.11)
18
+ activesupport (= 3.2.11)
19
+ builder (~> 3.0.0)
20
+ activerecord (3.2.11)
21
+ activemodel (= 3.2.11)
22
+ activesupport (= 3.2.11)
23
+ arel (~> 3.0.2)
24
+ tzinfo (~> 0.3.29)
25
+ activeresource (3.2.11)
26
+ activemodel (= 3.2.11)
27
+ activesupport (= 3.2.11)
28
+ activesupport (3.2.11)
29
+ i18n (~> 0.6)
30
+ multi_json (~> 1.0)
31
+ arel (3.0.2)
32
+ builder (3.0.4)
33
+ diff-lcs (1.1.3)
34
+ erubis (2.7.0)
35
+ factory_girl (4.1.0)
36
+ activesupport (>= 3.0.0)
37
+ git (1.2.5)
38
+ hike (1.2.1)
39
+ i18n (0.6.1)
40
+ jeweler (1.8.4)
41
+ bundler (~> 1.0)
42
+ git (>= 1.2.5)
43
+ rake
44
+ rdoc
45
+ journey (1.0.4)
46
+ json (1.7.6)
47
+ mail (2.4.4)
48
+ i18n (>= 0.4.0)
49
+ mime-types (~> 1.16)
50
+ treetop (~> 1.4.8)
51
+ mime-types (1.19)
52
+ multi_json (1.5.0)
53
+ polyglot (0.3.3)
54
+ rack (1.4.3)
55
+ rack-cache (1.2)
56
+ rack (>= 0.4)
57
+ rack-ssl (1.3.2)
58
+ rack
59
+ rack-test (0.6.2)
60
+ rack (>= 1.0)
61
+ rails (3.2.11)
62
+ actionmailer (= 3.2.11)
63
+ actionpack (= 3.2.11)
64
+ activerecord (= 3.2.11)
65
+ activeresource (= 3.2.11)
66
+ activesupport (= 3.2.11)
67
+ bundler (~> 1.0)
68
+ railties (= 3.2.11)
69
+ railties (3.2.11)
70
+ actionpack (= 3.2.11)
71
+ activesupport (= 3.2.11)
72
+ rack-ssl (~> 1.3.2)
73
+ rake (>= 0.8.7)
74
+ rdoc (~> 3.4)
75
+ thor (>= 0.14.6, < 2.0)
76
+ rake (10.0.3)
77
+ rdoc (3.12)
78
+ json (~> 1.4)
79
+ rspec (2.12.0)
80
+ rspec-core (~> 2.12.0)
81
+ rspec-expectations (~> 2.12.0)
82
+ rspec-mocks (~> 2.12.0)
83
+ rspec-core (2.12.2)
84
+ rspec-expectations (2.12.1)
85
+ diff-lcs (~> 1.1.3)
86
+ rspec-mocks (2.12.1)
87
+ simplecov (0.7.1)
88
+ multi_json (~> 1.0)
89
+ simplecov-html (~> 0.7.1)
90
+ simplecov-html (0.7.1)
91
+ sprockets (2.2.2)
92
+ hike (~> 1.2)
93
+ multi_json (~> 1.0)
94
+ rack (~> 1.0)
95
+ tilt (~> 1.1, != 1.3.0)
96
+ thor (0.16.0)
97
+ tilt (1.3.3)
98
+ treetop (1.4.12)
99
+ polyglot
100
+ polyglot (>= 0.3.1)
101
+ tzinfo (0.3.35)
102
+
103
+ PLATFORMS
104
+ ruby
105
+
106
+ DEPENDENCIES
107
+ bundler
108
+ factory_girl
109
+ jeweler
110
+ rails
111
+ rdoc
112
+ rspec
113
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Brian Goff
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,105 @@
1
+ # PackRat
2
+
3
+ PackRat is a simple helper for Rails.cache<br />
4
+ When included in your class it makes caching trivial.
5
+
6
+ class MyClass
7
+ include PackRat::CacheHelper
8
+
9
+ def some_method
10
+ cache do
11
+ #stuff
12
+ end
13
+ end
14
+
15
+ def self.some_class_method
16
+ cache do
17
+ # stuff
18
+ end
19
+ end
20
+ end
21
+
22
+ ## Installation
23
+ To install:
24
+
25
+ gem install pack_rat
26
+
27
+ or include in your Gemfile
28
+
29
+ gem 'pack_rat'
30
+
31
+ PackRat::CacheHelper does a few things:
32
+
33
+ * Adds a cache\_key method to your class, which takes your most recently updated record (assuming a model here) and calls it's cache\_key, and adds your classes name<br />
34
+ Right now cache_key assumes you are using ActiveRecord as your ORM, but you can overwrite this
35
+ * Adds a few of class attrs:
36
+ * file\_location - Defaults to using your model's name to determine the file's path, you can set this manually in your class `self.file_location = '/path/to/file'`
37
+ * updated\_attribute\_name - Defaults to :updated_at, this is used for your class's cache\_key method, `self.updated_attribute_name = :my_custom_attr`
38
+ * file\_digest - When your class is first loaded an MD5 digest is created, based on file\_location, this ensures that if your class changes, your caches automatically expire
39
+ * Adds a `cache` method to your class and your instances<br />
40
+ `cache` uses the calling method name, your class's md5 digest, and the calling context's cache\_key method<br />
41
+ You can pass in some options:
42
+ * overwrite\_key - When set, whatever key is passed into `cache` will overwrite the automatically generated key<br />
43
+ If a key is provided and overwrite_key is not set, it will get added to the auto generated key
44
+ * debug - When set, will output the generated cache key to the console
45
+ * accepts all other options available to <a href="https://www.google.com/url?url=http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html%23method-i-fetch&rct=j&q=Rails.cache.fetch&usg=AFQjCNGCOKoKf1oOCJpQsURoQfG6jNYgsw&sa=X&ei=PXDxUMCPBYz69gSP4IHwDQ&ved=0CDcQygQwAA">Rails.cache.fetch</a>
46
+
47
+ When included into your gemfile, PackRat is automatically included into ActiveRecord::Base
48
+
49
+ If you do not want an MD5 digest created, then set `self.file_location = nil` in your class.
50
+
51
+ Check it out. Use the `debug: true` option in your cache call to see what key is actually being generated for debugging
52
+
53
+ def some_method
54
+ cache '', debug: true do
55
+ # stuff
56
+ end
57
+ end
58
+
59
+ An Example using an ActiveRecord class
60
+
61
+ class MyClass < ActiveRecord::Base
62
+
63
+ def my_long_instance_method
64
+ cache do
65
+ puts 'stuff'
66
+ end
67
+ end
68
+
69
+ def self.my_long_class_method
70
+ cache do
71
+ puts 'stuff'
72
+ end
73
+ end
74
+
75
+ def method_with_debug
76
+ cache [], debug: true do
77
+ puts 'stuff'
78
+ end
79
+ end
80
+
81
+ def self.class_method_with_debug
82
+ cache [], debug: true do
83
+ puts 'stuff'
84
+ end
85
+ end
86
+ end
87
+
88
+ ### If you are passing in options, you must specify a cache key, this can be an empty string, or an empty array, or even not empty, but it has to be there
89
+
90
+
91
+ ## Contributing to pack_rat
92
+
93
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
94
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
95
+ * Fork the project.
96
+ * Start a feature/bugfix branch.
97
+ * Commit and push until you are happy with your contribution.
98
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
99
+ * 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.
100
+
101
+ ## Copyright
102
+
103
+ Copyright (c) 2013 Brian Goff. See LICENSE.txt for
104
+ further details.
105
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
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 = "pack_rat"
18
+ gem.homepage = "http://github.com/cpuguy83/pack_rat"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Rails cache helper}
21
+ gem.description = %Q{Helper method to simplify Rails caching, do Russian-doll caching outside your views}
22
+ gem.email = "cpuguy83@gmail.com"
23
+ gem.authors = ["Brian Goff"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "pack_rat #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/pack_rat.rb ADDED
@@ -0,0 +1,43 @@
1
+ module PackRat
2
+ module CacheHelper
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ extend Cacher
7
+ include Cacher
8
+ cattr_accessor :updated_attribute_name
9
+ self.updated_attribute_name ||= :updated_at
10
+ cattr_accessor :file_location
11
+ cattr_accessor :file_digest
12
+ self.file_location ||= "#{Rails.root}/app/models/#{self.to_s.split('::').join('/').underscore.downcase}.rb" if defined? Rails
13
+ self.file_digest ||= Digest::MD5.hexdigest(File.read(self.file_location)) if self.file_location
14
+ end
15
+
16
+ module Cacher
17
+ def cache(key='', options={}, &block)
18
+ unless options[:overwrite_key]
19
+ calling_method = caller[0][/`([^']*)'/, 1]
20
+ key << calling_method << '/'
21
+ key << self.cache_key << '/'
22
+ key << self.file_digest
23
+ end
24
+ puts key if options[:debug]
25
+ filtered_options = options.except(:overwrite_key, :debug)
26
+ Rails.cache.fetch key, filtered_options do
27
+ block.call
28
+ end
29
+ end
30
+ end
31
+
32
+ module ClassMethods
33
+ # Create cache_key for class, use most recently updated record
34
+ unless self.respond_to? :cache_key
35
+ define_method :cache_key do
36
+ key = order("#{self.updated_attribute_name} DESC").first.cache_key if self.superclass.to_s == "ActiveRecord::Base"
37
+ key << "/#{self.to_s}"
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ ActiveRecord::Base.send(:include, PackRat::CacheHelper) if defined? ActiveRecord::Base
data/pack_rat.gemspec ADDED
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "pack_rat"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brian Goff"]
12
+ s.date = "2013-01-12"
13
+ s.description = "Helper method to simplify Rails caching, do Russian-doll caching outside your views"
14
+ s.email = "cpuguy83@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/pack_rat.rb",
28
+ "pack_rat.gemspec",
29
+ "spec/spec_helper.rb"
30
+ ]
31
+ s.homepage = "http://github.com/cpuguy83/pack_rat"
32
+ s.licenses = ["MIT"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = "1.8.24"
35
+ s.summary = "Rails cache helper"
36
+
37
+ if s.respond_to? :specification_version then
38
+ s.specification_version = 3
39
+
40
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
+ s.add_runtime_dependency(%q<rails>, [">= 0"])
42
+ s.add_development_dependency(%q<rdoc>, [">= 0"])
43
+ s.add_development_dependency(%q<bundler>, [">= 0"])
44
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
45
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
46
+ else
47
+ s.add_dependency(%q<rails>, [">= 0"])
48
+ s.add_dependency(%q<rdoc>, [">= 0"])
49
+ s.add_dependency(%q<bundler>, [">= 0"])
50
+ s.add_dependency(%q<jeweler>, [">= 0"])
51
+ s.add_dependency(%q<simplecov>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<rails>, [">= 0"])
55
+ s.add_dependency(%q<rdoc>, [">= 0"])
56
+ s.add_dependency(%q<bundler>, [">= 0"])
57
+ s.add_dependency(%q<jeweler>, [">= 0"])
58
+ s.add_dependency(%q<simplecov>, [">= 0"])
59
+ end
60
+ end
61
+
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rspec'
11
+ require 'rspec/autorun'
12
+ require 'shoulda'
13
+ require 'factory_girl'
14
+
15
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
17
+ require 'pack_rat'
18
+
19
+ Rspec.configure do |config|
20
+ config.filter_run :focus => true
21
+ config.run_all_when_everything_filtered = true
22
+ config.treat_symbols_as_metadata_keys_with_true_values = true
23
+ config.include FactoryGirl::Syntax::Methods
24
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pack_rat
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Brian Goff
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ type: :runtime
16
+ name: rails
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ none: false
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ none: false
30
+ - !ruby/object:Gem::Dependency
31
+ type: :development
32
+ name: rdoc
33
+ requirement: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ none: false
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ none: false
46
+ - !ruby/object:Gem::Dependency
47
+ type: :development
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ none: false
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ none: false
62
+ - !ruby/object:Gem::Dependency
63
+ type: :development
64
+ name: jeweler
65
+ requirement: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ none: false
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ none: false
78
+ - !ruby/object:Gem::Dependency
79
+ type: :development
80
+ name: simplecov
81
+ requirement: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ none: false
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ none: false
94
+ description: Helper method to simplify Rails caching, do Russian-doll caching outside
95
+ your views
96
+ email: cpuguy83@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.md
102
+ files:
103
+ - .document
104
+ - Gemfile
105
+ - Gemfile.lock
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - VERSION
110
+ - lib/pack_rat.rb
111
+ - pack_rat.gemspec
112
+ - spec/spec_helper.rb
113
+ homepage: http://github.com/cpuguy83/pack_rat
114
+ licenses:
115
+ - MIT
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ segments:
126
+ - 0
127
+ hash: -3510960354438329384
128
+ none: false
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ none: false
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 1.8.24
138
+ signing_key:
139
+ specification_version: 3
140
+ summary: Rails cache helper
141
+ test_files: []