thisisdato-va_cache 1.0.0 → 1.0.1
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/Manifest +2 -0
- data/README.rdoc +6 -16
- data/init.rb +1 -0
- data/lib/virtual_attribute_cache.rb +1 -0
- data/lib/virtual_attribute_cache/class_methods.rb +12 -3
- data/lib/virtual_attribute_cache/version.rb +1 -1
- data/spec/spec_helper.rb +2 -6
- data/spec/virtual_attribute_cache/class_methods_spec.rb +7 -2
- data/va_cache.gemspec +3 -3
- metadata +3 -2
data/Manifest
CHANGED
|
@@ -5,9 +5,11 @@ lib/virtual_attribute_cache/version.rb
|
|
|
5
5
|
lib/virtual_attribute_cache/class_methods.rb
|
|
6
6
|
Rakefile
|
|
7
7
|
LICENSE.txt
|
|
8
|
+
init.rb
|
|
8
9
|
README.rdoc
|
|
9
10
|
spec/virtual_attribute_cache/class_methods_spec.rb
|
|
10
11
|
spec/virtual_attribute_cache/version_spec.rb
|
|
11
12
|
spec/blueprints.rb
|
|
12
13
|
spec/spec_helper.rb
|
|
13
14
|
Manifest
|
|
15
|
+
va_cache.gemspec
|
data/README.rdoc
CHANGED
|
@@ -14,27 +14,17 @@ As a Rails plugin:
|
|
|
14
14
|
|
|
15
15
|
== Usage
|
|
16
16
|
|
|
17
|
-
|
|
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
|
|
17
|
+
Virtual Attribute Cache extends Active Record with a +cached_virtual_attribute+
|
|
29
18
|
method for you to use in your models. Provide the name of the virtual attribute
|
|
30
19
|
to be cached and a proc, symbol, or string indicating when to expire the cache:
|
|
31
20
|
|
|
32
|
-
|
|
21
|
+
cached_virtual_attribute :full_name, :expire_if => { |p| p.first_name_changed? || p.last_name_changed? }
|
|
33
22
|
|
|
34
|
-
|
|
35
|
-
|
|
23
|
+
All you need is a column of the same name as the virtual attribute. In the
|
|
24
|
+
following case, if <tt>body_textile_changed?</tt> returns true, the cache will
|
|
25
|
+
be expired:
|
|
36
26
|
|
|
37
|
-
|
|
27
|
+
cached_virtual_attribute :body, :expire_if => :body_textile_changed
|
|
38
28
|
|
|
39
29
|
Note: Virtual Attribute Cache will suffix the question mark (?) automatically.
|
|
40
30
|
|
data/init.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'va_cache'
|
|
@@ -6,18 +6,27 @@ module VirtualAttributeCache
|
|
|
6
6
|
# proc that returns true with that particular record as argument, the cache
|
|
7
7
|
# will be expired:
|
|
8
8
|
#
|
|
9
|
-
#
|
|
9
|
+
# cached_virtual_attribute :full_name, :expire_if => Proc.new { |p| p.first_name_changed? || p.last_name_changed? }
|
|
10
10
|
#
|
|
11
11
|
# If it's not a proc, it will be converted to a string, suffixed with a
|
|
12
12
|
# question mark (?), and if that method returns true, the cache will be
|
|
13
13
|
# expired:
|
|
14
14
|
#
|
|
15
|
-
#
|
|
16
|
-
|
|
15
|
+
# cached_virtual_attribute :body, :expire_if => :body_textile_changed
|
|
16
|
+
#
|
|
17
|
+
# The cache will be stored in whatever database column that matches the
|
|
18
|
+
# virtual attribute's name. So you need one corresponding column for each
|
|
19
|
+
# virtual attribute you want to cache.
|
|
20
|
+
def cached_virtual_attribute(attribute, options = {}, &block)
|
|
21
|
+
define_method attribute do
|
|
22
|
+
self[attribute] || instance_eval(&block)
|
|
23
|
+
end
|
|
24
|
+
|
|
17
25
|
before_save do |record|
|
|
18
26
|
if (!record.attribute_present?(attribute)) ||
|
|
19
27
|
(options[:expire_if].is_a?(Proc) && options[:expire_if].call(record)) ||
|
|
20
28
|
(record.send "#{options[:expire_if]}?")
|
|
29
|
+
record[attribute] = nil
|
|
21
30
|
record.send "#{attribute}=", record.send(attribute)
|
|
22
31
|
end
|
|
23
32
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -21,17 +21,13 @@ ActiveRecord::Schema.define do
|
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
class Person < ActiveRecord::Base
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
def full_name
|
|
24
|
+
cached_virtual_attribute(:full_name, :expire_if => Proc.new { |p| p.first_name_changed? || p.last_name_changed? }) do
|
|
27
25
|
[first_name, last_name].join(' ')
|
|
28
26
|
end
|
|
29
27
|
end
|
|
30
28
|
|
|
31
29
|
class Post < ActiveRecord::Base
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
def body
|
|
30
|
+
cached_virtual_attribute(:body, :expire_if => :body_textile_changed) do
|
|
35
31
|
RedCloth.new(body_textile)
|
|
36
32
|
end
|
|
37
33
|
end
|
|
@@ -2,13 +2,18 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
|
|
2
2
|
|
|
3
3
|
module VirtualAttributeCache
|
|
4
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
|
+
|
|
5
11
|
it 'should cache the virtual attribute' do
|
|
6
12
|
person = Person.make_unsaved
|
|
7
13
|
person.attribute_present?(:full_name).should be_false
|
|
8
|
-
full_name = person.full_name
|
|
9
14
|
person.save
|
|
10
15
|
person.attribute_present?(:full_name).should be_true
|
|
11
|
-
person.full_name.should == full_name
|
|
16
|
+
person.full_name.should == person[:full_name]
|
|
12
17
|
end
|
|
13
18
|
|
|
14
19
|
it 'should expire the cache if :expire_if option is a proc and returns true' do
|
data/va_cache.gemspec
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
Gem::Specification.new do |s|
|
|
4
4
|
s.name = %q{va_cache}
|
|
5
|
-
s.version = "1.0.
|
|
5
|
+
s.version = "1.0.1"
|
|
6
6
|
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
|
8
8
|
s.authors = ["David Trasbo"]
|
|
9
|
-
s.date = %q{2009-08-
|
|
9
|
+
s.date = %q{2009-08-06}
|
|
10
10
|
s.description = %q{The ultimate way to cache Active Record virtual attributes.}
|
|
11
11
|
s.email = %q{thisisdato@gmail.com}
|
|
12
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"]
|
|
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", "init.rb", "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
14
|
s.homepage = %q{http://github.com/thisisdato/va_cache/tree/master}
|
|
15
15
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Va_cache", "--main", "README.rdoc"]
|
|
16
16
|
s.require_paths = ["lib"]
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: thisisdato-va_cache
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- David Trasbo
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date: 2009-08-
|
|
12
|
+
date: 2009-08-06 00:00:00 -07:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies: []
|
|
15
15
|
|
|
@@ -35,6 +35,7 @@ files:
|
|
|
35
35
|
- lib/virtual_attribute_cache/class_methods.rb
|
|
36
36
|
- Rakefile
|
|
37
37
|
- LICENSE.txt
|
|
38
|
+
- init.rb
|
|
38
39
|
- README.rdoc
|
|
39
40
|
- spec/virtual_attribute_cache/class_methods_spec.rb
|
|
40
41
|
- spec/virtual_attribute_cache/version_spec.rb
|