covercache 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzNkNTE2ZGNhMWY5NGZmYWMyZDY1NTE5NzhmYWQwZjlmZjEyMWVkOA==
5
+ data.tar.gz: !binary |-
6
+ ZWFlOWM5M2ZiZGFmZjE0NzAxNDVlYjVkMzU2YWM0ZGE2ODQ5ZDY4Mw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NDJhMjcxZGQzZWYwMTAxNWMxNTQxMDdhNTM3ZGMxNmU4MDgzYmYwYzJjN2Vm
10
+ YWJkNjE5N2JjNGRkMmYxODczYmI0MDZmODlhNzJiZTQ4ZDg4YjU1M2NhMjUz
11
+ ZDk5ODdmODk5MjVkNTUyODE5YTc1ZDhlYmI3OWZlMDhiZjI0MjY=
12
+ data.tar.gz: !binary |-
13
+ NmQ4MmJiNjIyNzM5ZjgwMGUwNWJiNzkyOTljMDEzYWVhMmE3MzMxNzI3ZmIz
14
+ NzdkYjFlNzNlYWRjNGI1MGJhZTY1YzJjN2Q1OWE4MmZjZWMwMjk2ZDkyM2Iw
15
+ ZjI1NGM4NmQ1NDcxNGMyNjQ3ODhlMzYwNTBlNWYyYjg3MzcwYmM=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in covercache.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tõnis Simo
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,55 @@
1
+ # Covercache
2
+
3
+ Covercache based on PackRat
4
+
5
+ Covercache is a simple helper for Rails.cache<br />
6
+ When included in your class it makes caching trivial.
7
+
8
+ Add following line to your model which you want to cache
9
+
10
+ class Post < ActiveRecord::Base
11
+ covers_with_cache
12
+
13
+ Now you can wrap methods with Covercash (like PackRat). For example:
14
+
15
+ has_many :comments
16
+ scope :published, where(published: true)
17
+
18
+ def cached_comments
19
+ covercache do
20
+ comments.all
21
+ end
22
+ end
23
+
24
+ def self.cached_published
25
+ covercache do
26
+ published.all
27
+ end
28
+ end
29
+
30
+
31
+ ## Installation
32
+
33
+ Add this line to your application's Gemfile:
34
+
35
+ gem 'covercache'
36
+
37
+ And then execute:
38
+
39
+ $ bundle
40
+
41
+ Or install it yourself as:
42
+
43
+ $ gem install covercache
44
+
45
+ ## Usage
46
+
47
+ TODO: Write usage instructions here
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ # require 'rake/testtask'
4
+ # Rake::TestTask.new(:test) do |test|
5
+ # test.libs << 'lib' << 'spec'
6
+ # test.pattern = 'spec/**/*_spec.rb'
7
+ # test.verbose = true
8
+ # end
9
+ #
10
+ # require 'rdoc/task'
11
+ # Rake::RDocTask.new do |rdoc|
12
+ # version = File.exist?('VERSION') ? File.read('VERSION') : ""
13
+ #
14
+ # rdoc.rdoc_dir = 'rdoc'
15
+ # rdoc.title = "covercashe #{version}"
16
+ # rdoc.rdoc_files.include('README*')
17
+ # rdoc.rdoc_files.include('lib/**/*.rb')
18
+ # end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'covercache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "covercache"
8
+ spec.version = Covercache::VERSION
9
+ spec.authors = ["Tõnis Simo", "Brian Goff"]
10
+ spec.email = ["anton.estum@gmail.com"]
11
+ spec.description = %q{Helper method to simplify Rails caching, based on PackRat}
12
+ spec.summary = %q{Rails cache helper based on PackRat gem}
13
+ spec.homepage = "http://github.com/tonissimo/covercache"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rails", "~> 3.2"
23
+ spec.add_development_dependency "rdoc"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "sqlite3"
27
+
28
+ spec.add_dependency "activesupport", "~> 3.0"
29
+ spec.add_dependency "activerecord", "~> 3.0"
30
+ end
@@ -0,0 +1,43 @@
1
+ module Covercache
2
+ module Model
3
+ module Cacher
4
+ private
5
+ def covercache(*args, &block)
6
+ options = args.extract_options!
7
+ key = []
8
+ should_debug = options[:debug].present? and options[:debug]
9
+
10
+ klass = covercaching_class_method? ? self : self.class
11
+
12
+ filtered_options = options.except(:debug, :overwrite_key) # Remove PackRat related options so we can pass to Rails.cache
13
+
14
+ unless options[:overwrite_key] # if overwrite_key was set, we skip creating our own key
15
+ model_digest = covercaching_get_model_digest
16
+ calling_method = caller[0][/`([^']*)'/, 1] # Hack to get the method that called cache
17
+
18
+ key << klass.name
19
+ key << [model_digest, calling_method].compact.join('/')
20
+ key << cache_key if self.respond_to?(:cache_key)
21
+ key += args
22
+ end
23
+
24
+ puts key.inspect if should_debug # Output the generated cache key to the console if debug is set
25
+
26
+ # Make the actual Rails.cache call
27
+ Rails.cache.fetch key, filtered_options do
28
+ klass.covercache_keys << key unless key.in?(klass.covercache_keys)
29
+ block.call
30
+ end
31
+ end
32
+
33
+ private
34
+ def covercaching_get_model_digest
35
+ self.covercache_model_digest.presence
36
+ end
37
+
38
+ def covercaching_class_method?
39
+ self.is_a? Class
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_support/concern'
2
+ require 'covercache/model/cacher'
3
+
4
+ module Covercache
5
+ module Model
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ extend Cacher # Include and Extend so cache method is available in all contexts
10
+ include Cacher
11
+ end
12
+
13
+ module ClassMethods
14
+ def generate_model_digest
15
+ return unless covercache_model_source?
16
+ file = File.read self.covercache_model_source
17
+ Digest::MD5.hexdigest(file)
18
+ rescue
19
+ nil
20
+ end
21
+
22
+ # Generates and sets file_digest attribute
23
+ def generate_model_digest!
24
+ self.covercache_model_digest = self.generate_model_digest
25
+ end
26
+
27
+ def covercache_flush!
28
+ self.covercache_keys.each do |key|
29
+ Rails.cache.delete(key) # if Rails.cache.exist?(key)
30
+ end.clear
31
+ self.covercache_keys.empty?
32
+ end
33
+ end
34
+
35
+ # flush cache on after_commit callback
36
+ def covercache_flush_cache!
37
+ self.class.send :covercache_flush!
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module Covercache
2
+ VERSION = "0.0.1"
3
+ end
data/lib/covercache.rb ADDED
@@ -0,0 +1,44 @@
1
+ require "where"
2
+ require 'active_record'
3
+ require 'active_support/core_ext'
4
+ require "covercache/version"
5
+ require "covercache/model"
6
+
7
+ module Covercache
8
+ module CoversWithCache
9
+ def covers_with_cache
10
+ class_eval do
11
+ %w(keys model_source model_digest).each do |key, value|
12
+ class_attribute :"covercache_#{key}"
13
+ self.send(:"covercache_#{key}=", value) if value.present?
14
+ end
15
+
16
+ self.covercache_keys ||= []
17
+ self.covercache_model_source = Where.is_class self, of: 'app/models'
18
+
19
+ include Covercache::Model
20
+
21
+ generate_model_digest!
22
+
23
+ after_commit :covercache_flush_cache
24
+ end
25
+ end
26
+
27
+ # TODO: coming soon...
28
+ def define_cached(*args)
29
+ method_name = args.shift
30
+ opts = args.extract_options!
31
+ define_method :"cached_#{method_name}" do |*method_args|
32
+ puts self.inspect
33
+ if method_args.last.is_a?(Hash) and method_args.last.has_key?(:cache_key)
34
+ add_to_args = method_args.last.delete(:cache_key)
35
+ args += [add_to_args].flatten if add_to_args.present?
36
+ end
37
+ covercache(*args, opts) { self.send(method_name, *method_args) }
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+
44
+ ActiveRecord::Base.extend Covercache::CoversWithCache
data/lib/where.rb ADDED
@@ -0,0 +1,69 @@
1
+ # A little Ruby module for finding the source location where class and methods are defined.
2
+ # https://gist.github.com/wtaysom/1236979
3
+
4
+ module Where
5
+ #== Example
6
+ #
7
+ # Where.is_class Post, of: 'app/models'
8
+ #
9
+ class <<self
10
+ def is_class(klass, opts={})
11
+ methods = defined_methods(klass)
12
+ file_groups = methods.group_by{|sl| sl[0]}
13
+ file_counts = file_groups.map do |file, sls|
14
+ lines = sls.map{|sl| sl[1]}
15
+ count = lines.size
16
+ line = lines.min
17
+ {file: file, count: count, line: line}
18
+ end
19
+ file_counts.sort_by!{|fc| fc[:count]}
20
+
21
+ if opts[:of].present?
22
+ path_expand = joins_path(opts[:of])
23
+ return File.expand_path("../../spec/spec_helper.rb", __FILE__) unless !!path_expand
24
+ pattern = Regexp.new "^#{path_expand}/.*"
25
+ matches = file_counts.select do |fc|
26
+ !!fc[:file][pattern]
27
+ end
28
+ return matches.first[:file] if matches.first.present?
29
+ end
30
+
31
+ source_locations = file_counts.map{|fc| [fc[:file], fc[:line]]}
32
+ source_locations
33
+ end
34
+
35
+ private
36
+ def joins_path(of)
37
+ Rails.root.join(of).to_s
38
+ rescue NameError
39
+ return false
40
+ end
41
+
42
+ def source_location(method)
43
+ method.source_location || (
44
+ method.to_s =~ /: (.*)>/
45
+ $1
46
+ )
47
+ end
48
+
49
+ def defined_methods(klass)
50
+ methods = klass.methods(false).map{|m| klass.method(m)} +
51
+ klass.instance_methods(false).map{|m| klass.instance_method(m)}
52
+ methods.map!(&:source_location)
53
+ methods.compact!
54
+ methods
55
+ end
56
+ end
57
+ end
58
+
59
+ def where_is(klass, method = nil)
60
+ Where.edit(if method
61
+ begin
62
+ Where.is_instance_method(klass, method)
63
+ rescue NameError
64
+ Where.is_method(klass, method)
65
+ end
66
+ else
67
+ Where.is_class_primarily(klass)
68
+ end)
69
+ end
Binary file
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'pp'
3
+
4
+ describe "covercache" do
5
+ it 'should respond to covers_with_cache' do
6
+ Post.should respond_to(:covers_with_cache)
7
+ end
8
+ it 'should hove class keys storage' do
9
+ pp Post.covercache_keys
10
+ Post.covercache_keys.should be_an(Array)
11
+ end
12
+ it 'should have covercache_model_source attribute' do
13
+ pp Post.covercache_model_source
14
+ Post.covercache_model_source.should be_an(String)
15
+ end
16
+ it 'should have covercache_model_digest attribute' do
17
+ pp Post.covercache_model_digest
18
+ Post.covercache_model_digest.should be_an(String)
19
+ end
20
+ it 'should return the same digest for class and instance' do
21
+ post1 = Post.find(1)
22
+ pp post1.covercache_model_digest
23
+ Post.covercache_model_digest.should == post1.covercache_model_digest
24
+ end
25
+ it 'should return the same values with or without cache' do
26
+ test = Post.find(1)
27
+ pp test.inspect
28
+ pp test.cached_comments.inspect
29
+ test.cached_comments.count.should == test.comments.count
30
+ end
31
+ end
@@ -0,0 +1,81 @@
1
+ require 'rubygems'
2
+ require 'covercache'
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'rspec'
12
+ require 'rspec/autorun'
13
+
14
+ ActiveRecord::Base.establish_connection adapter: "sqlite3",
15
+ database: File.expand_path("../covercache.sqlite3", __FILE__)
16
+
17
+ class Rails
18
+ def self.cache
19
+ Rails::Cache
20
+ end
21
+ end
22
+
23
+ class Rails::Cache
24
+ def self.fetch(*options, &block)
25
+ block.call
26
+ end
27
+ end
28
+
29
+ # This file was generated by the `rspec --init` command. Conventionally, all
30
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
31
+ # Require this file using `require "spec_helper"` to ensure that it is only
32
+ # loaded once.
33
+ #
34
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
35
+ RSpec.configure do |config|
36
+ config.treat_symbols_as_metadata_keys_with_true_values = true
37
+ config.run_all_when_everything_filtered = true
38
+ config.filter_run :focus
39
+
40
+ # Run specs in random order to surface order dependencies. If you find an
41
+ # order dependency and want to debug it, you can fix the order by providing
42
+ # the seed, which is printed after each run.
43
+ # --seed 1234
44
+ # config.order = 'random'
45
+ end
46
+
47
+ ActiveRecord::Schema.define do
48
+ self.verbose = false
49
+
50
+ create_table :posts, :force => true do |t|
51
+ t.string :text
52
+ t.timestamps
53
+ end
54
+
55
+ create_table :comments, :force => true do |t|
56
+ t.belongs_to :post
57
+ t.string :text
58
+ t.timestamps
59
+ end
60
+ end
61
+
62
+ class Post < ActiveRecord::Base
63
+ has_many :comments
64
+ covers_with_cache
65
+ def cached_comments
66
+ covercache debug: true do
67
+ comments.all
68
+ end
69
+ end
70
+ end
71
+
72
+ class Comment < ActiveRecord::Base
73
+ belongs_to :post
74
+ covers_with_cache
75
+ end
76
+
77
+ post1 = Post.create(:text => "First post!")
78
+ 10.times {|i| post1.comments.create(text: "Comment ##{i} for first post") }
79
+
80
+ post2 = Post.create(:text => "Second post!")
81
+ 6.times {|i| post2.comments.create(text: "Comment ##{i} for second post") }
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: covercache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tõnis Simo
8
+ - Brian Goff
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rails
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '3.2'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '3.2'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rdoc
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: sqlite3
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: activesupport
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ~>
103
+ - !ruby/object:Gem::Version
104
+ version: '3.0'
105
+ type: :runtime
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ~>
110
+ - !ruby/object:Gem::Version
111
+ version: '3.0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: activerecord
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ~>
117
+ - !ruby/object:Gem::Version
118
+ version: '3.0'
119
+ type: :runtime
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: '3.0'
126
+ description: Helper method to simplify Rails caching, based on PackRat
127
+ email:
128
+ - anton.estum@gmail.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - .rspec
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - covercache.gemspec
140
+ - lib/covercache.rb
141
+ - lib/covercache/model.rb
142
+ - lib/covercache/model/cacher.rb
143
+ - lib/covercache/version.rb
144
+ - lib/where.rb
145
+ - spec/covercache.sqlite3
146
+ - spec/libs/covercache_spec.rb
147
+ - spec/spec_helper.rb
148
+ homepage: http://github.com/tonissimo/covercache
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ! '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.0.3
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Rails cache helper based on PackRat gem
172
+ test_files:
173
+ - spec/covercache.sqlite3
174
+ - spec/libs/covercache_spec.rb
175
+ - spec/spec_helper.rb