thisisdato-va_cache 1.0.0
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/CHANGELOG.rdoc +3 -0
- data/LICENSE.txt +22 -0
- data/Manifest +13 -0
- data/README.rdoc +44 -0
- data/Rakefile +17 -0
- data/lib/va_cache.rb +1 -0
- data/lib/virtual_attribute_cache/class_methods.rb +26 -0
- data/lib/virtual_attribute_cache/version.rb +9 -0
- data/lib/virtual_attribute_cache.rb +2 -0
- data/spec/blueprints.rb +12 -0
- data/spec/spec_helper.rb +39 -0
- data/spec/virtual_attribute_cache/class_methods_spec.rb +31 -0
- data/spec/virtual_attribute_cache/version_spec.rb +9 -0
- data/va_cache.gemspec +30 -0
- metadata +78 -0
data/CHANGELOG.rdoc
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2009 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/Manifest
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
CHANGELOG.rdoc
|
|
2
|
+
lib/va_cache.rb
|
|
3
|
+
lib/virtual_attribute_cache.rb
|
|
4
|
+
lib/virtual_attribute_cache/version.rb
|
|
5
|
+
lib/virtual_attribute_cache/class_methods.rb
|
|
6
|
+
Rakefile
|
|
7
|
+
LICENSE.txt
|
|
8
|
+
README.rdoc
|
|
9
|
+
spec/virtual_attribute_cache/class_methods_spec.rb
|
|
10
|
+
spec/virtual_attribute_cache/version_spec.rb
|
|
11
|
+
spec/blueprints.rb
|
|
12
|
+
spec/spec_helper.rb
|
|
13
|
+
Manifest
|
data/README.rdoc
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
As a Rails plugin:
|
|
12
|
+
|
|
13
|
+
script/plugin install git://github.com/thisisdato/va_cache.git
|
|
14
|
+
|
|
15
|
+
== Usage
|
|
16
|
+
|
|
17
|
+
The basic concept of Virtual Attribute Cache is as follows: If you have a
|
|
18
|
+
+Person+ model with +first_name+, +last_name+, and +full_name+ columns and a
|
|
19
|
+
+full_name+ method (virtual attribute), and the +full_name+ column is blank in
|
|
20
|
+
the database, the +full_name+ method will not be overridden by Active Record.
|
|
21
|
+
However, if the +full_name+ column is not blank, Active Record will override the
|
|
22
|
+
+full_name+ method to return that.
|
|
23
|
+
|
|
24
|
+
This behaviour allows you to cache virtual attributes simply by having a
|
|
25
|
+
database column of the same name and use Virtual Attribute Cache to expire the
|
|
26
|
+
cache as neseccary.
|
|
27
|
+
|
|
28
|
+
Virtual Attribute Cache extends Active Record with a caches_virtual_attribute
|
|
29
|
+
method for you to use in your models. Provide the name of the virtual attribute
|
|
30
|
+
to be cached and a proc, symbol, or string indicating when to expire the cache:
|
|
31
|
+
|
|
32
|
+
caches_virtual_attribute :full_name, :expire_if => { |p| p.first_name_changed? || p.last_name_changed? }
|
|
33
|
+
|
|
34
|
+
In the following case, if <tt>body_textile_changed?</tt> returns true, the cache
|
|
35
|
+
will be expired:
|
|
36
|
+
|
|
37
|
+
caches_virtual_attribute :body, :expire_if => :body_textile_changed
|
|
38
|
+
|
|
39
|
+
Note: Virtual Attribute Cache will suffix the question mark (?) automatically.
|
|
40
|
+
|
|
41
|
+
== Copyright
|
|
42
|
+
|
|
43
|
+
Copyright (c) 2009 David Trasbo. Virtual Attribute Cache is released under the
|
|
44
|
+
terms of the MIT License. See LICENSE.txt for further details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'echoe'
|
|
3
|
+
require File.join(File.dirname(__FILE__), 'lib', 'virtual_attribute_cache', 'version')
|
|
4
|
+
require 'spec/rake/spectask'
|
|
5
|
+
|
|
6
|
+
Echoe.new('va_cache', VirtualAttributeCache::Version::STRING) do |s|
|
|
7
|
+
s.summary = 'The ultimate way to cache Active Record virtual attributes.'
|
|
8
|
+
s.url = 'http://github.com/thisisdato/va_cache/tree/master'
|
|
9
|
+
s.author = 'David Trasbo'
|
|
10
|
+
s.email = 'thisisdato@gmail.com'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
Spec::Rake::SpecTask.new
|
|
14
|
+
|
|
15
|
+
Spec::Rake::SpecTask.new('rcov') do |t|
|
|
16
|
+
t.rcov = true
|
|
17
|
+
end
|
data/lib/va_cache.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'virtual_attribute_cache')
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
# caches_virtual_attribute :full_name, :expire_if => Proc.new { |p| p.first_name_changed? || p.last_name_changed? }
|
|
10
|
+
#
|
|
11
|
+
# If it's not a proc, it will be converted to a string, suffixed with a
|
|
12
|
+
# question mark (?), and if that method returns true, the cache will be
|
|
13
|
+
# expired:
|
|
14
|
+
#
|
|
15
|
+
# caches_virtual_attribute :body, :expire_if => :body_textile_changed
|
|
16
|
+
def caches_virtual_attribute(attribute, options = {})
|
|
17
|
+
before_save do |record|
|
|
18
|
+
if (!record.attribute_present?(attribute)) ||
|
|
19
|
+
(options[:expire_if].is_a?(Proc) && options[:expire_if].call(record)) ||
|
|
20
|
+
(record.send "#{options[:expire_if]}?")
|
|
21
|
+
record.send "#{attribute}=", record.send(attribute)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
data/spec/blueprints.rb
ADDED
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
caches_virtual_attribute :full_name, :expire_if => Proc.new { |p| p.first_name_changed? || p.last_name_changed? }
|
|
25
|
+
|
|
26
|
+
def full_name
|
|
27
|
+
[first_name, last_name].join(' ')
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
class Post < ActiveRecord::Base
|
|
32
|
+
caches_virtual_attribute :body, :expire_if => :body_textile_changed
|
|
33
|
+
|
|
34
|
+
def body
|
|
35
|
+
RedCloth.new(body_textile)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
require File.join(File.dirname(__FILE__), 'blueprints')
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
|
2
|
+
|
|
3
|
+
module VirtualAttributeCache
|
|
4
|
+
describe ClassMethods do
|
|
5
|
+
it 'should cache the virtual attribute' do
|
|
6
|
+
person = Person.make_unsaved
|
|
7
|
+
person.attribute_present?(:full_name).should be_false
|
|
8
|
+
full_name = person.full_name
|
|
9
|
+
person.save
|
|
10
|
+
person.attribute_present?(:full_name).should be_true
|
|
11
|
+
person.full_name.should == full_name
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it 'should expire the cache if :expire_if option is a proc and returns true' do
|
|
15
|
+
person = Person.make
|
|
16
|
+
person.first_name = Faker::Name.first_name
|
|
17
|
+
person.last_name = Faker::Name.last_name
|
|
18
|
+
person.save
|
|
19
|
+
person.attribute_present?(:full_name).should be_true
|
|
20
|
+
person.full_name.should == [person.first_name, person.last_name].join(' ')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'should expire the cache if method indicated in :expire_if option returns true' do
|
|
24
|
+
post = Post.make
|
|
25
|
+
post.body_textile = 'Hello _again,_ *Textile!*'
|
|
26
|
+
post.save
|
|
27
|
+
post.attribute_present?(:body).should be_true
|
|
28
|
+
post.body.should == RedCloth.new(post.body_textile)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
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,30 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = %q{va_cache}
|
|
5
|
+
s.version = "1.0.0"
|
|
6
|
+
|
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
|
8
|
+
s.authors = ["David Trasbo"]
|
|
9
|
+
s.date = %q{2009-08-05}
|
|
10
|
+
s.description = %q{The ultimate way to cache Active Record virtual attributes.}
|
|
11
|
+
s.email = %q{thisisdato@gmail.com}
|
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG.rdoc", "lib/va_cache.rb", "lib/virtual_attribute_cache.rb", "lib/virtual_attribute_cache/version.rb", "lib/virtual_attribute_cache/class_methods.rb", "LICENSE.txt", "README.rdoc"]
|
|
13
|
+
s.files = ["CHANGELOG.rdoc", "lib/va_cache.rb", "lib/virtual_attribute_cache.rb", "lib/virtual_attribute_cache/version.rb", "lib/virtual_attribute_cache/class_methods.rb", "Rakefile", "LICENSE.txt", "README.rdoc", "spec/virtual_attribute_cache/class_methods_spec.rb", "spec/virtual_attribute_cache/version_spec.rb", "spec/blueprints.rb", "spec/spec_helper.rb", "Manifest", "va_cache.gemspec"]
|
|
14
|
+
s.homepage = %q{http://github.com/thisisdato/va_cache/tree/master}
|
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Va_cache", "--main", "README.rdoc"]
|
|
16
|
+
s.require_paths = ["lib"]
|
|
17
|
+
s.rubyforge_project = %q{va_cache}
|
|
18
|
+
s.rubygems_version = %q{1.3.5}
|
|
19
|
+
s.summary = %q{The ultimate way to cache Active Record virtual attributes.}
|
|
20
|
+
|
|
21
|
+
if s.respond_to? :specification_version then
|
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
23
|
+
s.specification_version = 3
|
|
24
|
+
|
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
26
|
+
else
|
|
27
|
+
end
|
|
28
|
+
else
|
|
29
|
+
end
|
|
30
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: thisisdato-va_cache
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- David Trasbo
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-08-05 00:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description: The ultimate way to cache Active Record virtual attributes.
|
|
17
|
+
email: thisisdato@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- CHANGELOG.rdoc
|
|
24
|
+
- lib/va_cache.rb
|
|
25
|
+
- lib/virtual_attribute_cache.rb
|
|
26
|
+
- lib/virtual_attribute_cache/version.rb
|
|
27
|
+
- lib/virtual_attribute_cache/class_methods.rb
|
|
28
|
+
- LICENSE.txt
|
|
29
|
+
- README.rdoc
|
|
30
|
+
files:
|
|
31
|
+
- CHANGELOG.rdoc
|
|
32
|
+
- lib/va_cache.rb
|
|
33
|
+
- lib/virtual_attribute_cache.rb
|
|
34
|
+
- lib/virtual_attribute_cache/version.rb
|
|
35
|
+
- lib/virtual_attribute_cache/class_methods.rb
|
|
36
|
+
- Rakefile
|
|
37
|
+
- LICENSE.txt
|
|
38
|
+
- README.rdoc
|
|
39
|
+
- spec/virtual_attribute_cache/class_methods_spec.rb
|
|
40
|
+
- spec/virtual_attribute_cache/version_spec.rb
|
|
41
|
+
- spec/blueprints.rb
|
|
42
|
+
- spec/spec_helper.rb
|
|
43
|
+
- Manifest
|
|
44
|
+
- va_cache.gemspec
|
|
45
|
+
has_rdoc: false
|
|
46
|
+
homepage: http://github.com/thisisdato/va_cache/tree/master
|
|
47
|
+
licenses:
|
|
48
|
+
post_install_message:
|
|
49
|
+
rdoc_options:
|
|
50
|
+
- --line-numbers
|
|
51
|
+
- --inline-source
|
|
52
|
+
- --title
|
|
53
|
+
- Va_cache
|
|
54
|
+
- --main
|
|
55
|
+
- README.rdoc
|
|
56
|
+
require_paths:
|
|
57
|
+
- lib
|
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: "0"
|
|
63
|
+
version:
|
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: "1.2"
|
|
69
|
+
version:
|
|
70
|
+
requirements: []
|
|
71
|
+
|
|
72
|
+
rubyforge_project: va_cache
|
|
73
|
+
rubygems_version: 1.3.5
|
|
74
|
+
signing_key:
|
|
75
|
+
specification_version: 3
|
|
76
|
+
summary: The ultimate way to cache Active Record virtual attributes.
|
|
77
|
+
test_files: []
|
|
78
|
+
|