va_cache 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/CHANGELOG.rdoc +12 -0
- data/LICENSE +22 -0
- data/README.rdoc +40 -0
- data/Rakefile +23 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/va_cache.rb +1 -0
- data/lib/virtual_attribute_cache/class_methods.rb +39 -0
- data/lib/virtual_attribute_cache/version.rb +9 -0
- data/lib/virtual_attribute_cache.rb +3 -0
- data/spec/blueprints.rb +11 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/virtual_attribute_cache/class_methods_spec.rb +47 -0
- data/spec/virtual_attribute_cache/version_spec.rb +9 -0
- data/va_cache.gemspec +58 -0
- metadata +74 -0
data/.gitignore
ADDED
data/CHANGELOG.rdoc
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2010 David Trasbo
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
= Virtual Attribute Cache
|
2
|
+
|
3
|
+
The ultimate way to cache Active Record virtual attributes.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
As a RubyGem:
|
8
|
+
|
9
|
+
gem install thisisdato-va_cache
|
10
|
+
|
11
|
+
(Note: GitHub still hosts va_cache with my old username.)
|
12
|
+
|
13
|
+
As a Rails plugin:
|
14
|
+
|
15
|
+
script/plugin install git://github.com/dtrasbo/va_cache.git
|
16
|
+
|
17
|
+
== Usage
|
18
|
+
|
19
|
+
Virtual Attribute Cache extends Active Record with a +cached_virtual_attribute+
|
20
|
+
method for you to use in your models. Provide the name of the virtual attribute
|
21
|
+
to be cached and a proc, symbol, or string indicating when to expire the cache:
|
22
|
+
|
23
|
+
cached_virtual_attribute(:full_name, :expire_if => Proc.new { |p| p.first_name_changed? || p.last_name_changed? }) do
|
24
|
+
[first_name, last_name].join(' ')
|
25
|
+
end
|
26
|
+
|
27
|
+
All you need is a column of the same name as the virtual attribute. In the
|
28
|
+
following case, if <tt>body_textile_changed?</tt> returns true, the cache will
|
29
|
+
be expired:
|
30
|
+
|
31
|
+
cached_virtual_attribute(:body, :expire_if => :body_textile_changed) do
|
32
|
+
RedCloth.new(body_textile).to_html
|
33
|
+
end
|
34
|
+
|
35
|
+
Note: Virtual Attribute Cache will suffix the question mark (?) automatically.
|
36
|
+
|
37
|
+
== Copyright
|
38
|
+
|
39
|
+
Copyright (c) 2010 David Trasbo. Virtual Attribute Cache is released under the
|
40
|
+
terms of the MIT License. See LICENSE.txt for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "va_cache"
|
5
|
+
gemspec.summary = "The ultimate way to cache Active Record virtual attributes."
|
6
|
+
gemspec.email = "thisisdato@gmail.com"
|
7
|
+
gemspec.homepage = "http://github.com/dtrasbo/va_cache"
|
8
|
+
gemspec.authors = ["David Trasbo"]
|
9
|
+
end
|
10
|
+
Jeweler::GemcutterTasks.new
|
11
|
+
rescue LoadError
|
12
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'spec/rake/spectask'
|
16
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
17
|
+
spec.libs << 'lib' << 'spec'
|
18
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
19
|
+
end
|
20
|
+
|
21
|
+
task :spec => :check_dependencies
|
22
|
+
|
23
|
+
task :default => :spec
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.2
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'va_cache'
|
data/lib/va_cache.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'virtual_attribute_cache')
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module VirtualAttributeCache
|
2
|
+
module ClassMethods
|
3
|
+
# Sets up a +before_save+ callback that will cache (or expire the cache of)
|
4
|
+
# the virtual attribute indicated by +attribute+. Use the
|
5
|
+
# <tt>:expire_if</tt> to specify when the cache will be expired. If it's a
|
6
|
+
# proc that returns true with that particular record as argument, the cache
|
7
|
+
# will be expired:
|
8
|
+
#
|
9
|
+
# cached_virtual_attribute(:full_name, :expire_if => Proc.new { |p| p.first_name_changed? || p.last_name_changed? }) do
|
10
|
+
# [first_name, last_name].join(' ')
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# If it's not a proc, it will be converted to a string, suffixed with a
|
14
|
+
# question mark (?), and if that method returns true, the cache will be
|
15
|
+
# expired:
|
16
|
+
#
|
17
|
+
# cached_virtual_attribute(:body, :expire_if => :body_textile_changed) do
|
18
|
+
# RedCloth.new(body_textile).to_html
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# The cache will be stored in whatever database column that matches the
|
22
|
+
# virtual attribute's name. So you need one corresponding column for each
|
23
|
+
# virtual attribute you want to cache.
|
24
|
+
def cached_virtual_attribute(attribute, options = {}, &block)
|
25
|
+
define_method attribute do
|
26
|
+
self[attribute] || instance_eval(&block)
|
27
|
+
end
|
28
|
+
|
29
|
+
before_save do |record|
|
30
|
+
if (!record.attribute_present?(attribute)) ||
|
31
|
+
(!options[:expire_if].is_a?(Proc) && record.send("#{options[:expire_if]}?")) ||
|
32
|
+
(options[:expire_if].is_a?(Proc) && options[:expire_if].call(record))
|
33
|
+
record[attribute] = nil
|
34
|
+
record.send "#{attribute}=", record.send(attribute)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/spec/blueprints.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'activerecord'
|
3
|
+
require 'RedCloth'
|
4
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'va_cache')
|
5
|
+
|
6
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
7
|
+
|
8
|
+
ActiveRecord::Schema.define do
|
9
|
+
suppress_messages do
|
10
|
+
create_table :people do |t|
|
11
|
+
t.string :first_name
|
12
|
+
t.string :last_name
|
13
|
+
t.string :full_name
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table :posts do |t|
|
17
|
+
t.string :body_textile
|
18
|
+
t.string :body
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Person < ActiveRecord::Base
|
24
|
+
cached_virtual_attribute(:full_name, :expire_if => Proc.new { |p| p.first_name_changed? || p.last_name_changed? }) do
|
25
|
+
[first_name, last_name].join(' ')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Post < ActiveRecord::Base
|
30
|
+
cached_virtual_attribute(:body, :expire_if => :body_textile_changed) do
|
31
|
+
RedCloth.new(body_textile).to_html
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
require File.join(File.dirname(__FILE__), 'blueprints')
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
2
|
+
|
3
|
+
module VirtualAttributeCache
|
4
|
+
describe ClassMethods do
|
5
|
+
it 'should override the virtual attribute to use cached version if present' do
|
6
|
+
person = Person.make
|
7
|
+
person.should_not_receive(:instance_eval)
|
8
|
+
person.full_name
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should cache the virtual attribute' do
|
12
|
+
person = Person.make_unsaved
|
13
|
+
person.attribute_present?(:full_name).should be_false
|
14
|
+
person.save
|
15
|
+
person.attribute_present?(:full_name).should be_true
|
16
|
+
person.full_name.should == person[:full_name]
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should expire the cache if :expire_if option is a proc and returns true' do
|
20
|
+
person = Person.make
|
21
|
+
person.first_name = 'James'
|
22
|
+
person.last_name = 'Williams'
|
23
|
+
person.save
|
24
|
+
person[:full_name].should == [person.first_name, person.last_name].join(' ')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should expire the cache if method indicated in :expire_if option returns true' do
|
28
|
+
post = Post.make
|
29
|
+
post.body_textile = 'Hello _again,_ *Textile!*'
|
30
|
+
post.save
|
31
|
+
post[:body].should == '<p>Hello <em>again,</em> <strong>Textile!</strong></p>'
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should not try to send "#{options[:expire_if]}?" when it is a Proc' do
|
35
|
+
person = Person.make
|
36
|
+
person.first_name = 'John'
|
37
|
+
person.last_name = 'Smith'
|
38
|
+
person.save
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should not try to #call a symbol' do
|
42
|
+
post = Post.make
|
43
|
+
post.body_textile = '_Hello_ *Textile!*'
|
44
|
+
post.save
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
2
|
+
|
3
|
+
module VirtualAttributeCache
|
4
|
+
describe Version do
|
5
|
+
it 'should join major, minor, and tiny revisions' do
|
6
|
+
Version::STRING.should == [Version::MAJOR, Version::MINOR, Version::TINY].join('.')
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
data/va_cache.gemspec
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{va_cache}
|
8
|
+
s.version = "1.0.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["David Trasbo"]
|
12
|
+
s.date = %q{2010-01-06}
|
13
|
+
s.email = %q{thisisdato@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE",
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"CHANGELOG.rdoc",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"init.rb",
|
26
|
+
"lib/va_cache.rb",
|
27
|
+
"lib/virtual_attribute_cache.rb",
|
28
|
+
"lib/virtual_attribute_cache/class_methods.rb",
|
29
|
+
"lib/virtual_attribute_cache/version.rb",
|
30
|
+
"spec/blueprints.rb",
|
31
|
+
"spec/spec_helper.rb",
|
32
|
+
"spec/virtual_attribute_cache/class_methods_spec.rb",
|
33
|
+
"spec/virtual_attribute_cache/version_spec.rb",
|
34
|
+
"va_cache.gemspec"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/dtrasbo/va_cache}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.5}
|
40
|
+
s.summary = %q{The ultimate way to cache Active Record virtual attributes.}
|
41
|
+
s.test_files = [
|
42
|
+
"spec/spec_helper.rb",
|
43
|
+
"spec/blueprints.rb",
|
44
|
+
"spec/virtual_attribute_cache/version_spec.rb",
|
45
|
+
"spec/virtual_attribute_cache/class_methods_spec.rb"
|
46
|
+
]
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
|
+
s.specification_version = 3
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
53
|
+
else
|
54
|
+
end
|
55
|
+
else
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: va_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Trasbo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-06 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: thisisdato@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- CHANGELOG.rdoc
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- init.rb
|
33
|
+
- lib/va_cache.rb
|
34
|
+
- lib/virtual_attribute_cache.rb
|
35
|
+
- lib/virtual_attribute_cache/class_methods.rb
|
36
|
+
- lib/virtual_attribute_cache/version.rb
|
37
|
+
- spec/blueprints.rb
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
- spec/virtual_attribute_cache/class_methods_spec.rb
|
40
|
+
- spec/virtual_attribute_cache/version_spec.rb
|
41
|
+
- va_cache.gemspec
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/dtrasbo/va_cache
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.5
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: The ultimate way to cache Active Record virtual attributes.
|
70
|
+
test_files:
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
- spec/blueprints.rb
|
73
|
+
- spec/virtual_attribute_cache/version_spec.rb
|
74
|
+
- spec/virtual_attribute_cache/class_methods_spec.rb
|