public
Rubygem
Description: Rails Plugin - memoize and store to the db a record attribute generated by any expression or SQL query.
Homepage: http://6brand.com
Clone URL: git://github.com/JackDanger/cached_values.git
okay, so I don't know how to merge two non-related repositories yet.  just 
copying files from cached_values_gem to the plugin one.  There's no reason 
to have both a plugin and a gem when I can have them together.
JackDanger (author)
Thu Mar 06 08:55:51 -0800 2008
commit  bfb84e2b422cab3a747268a42920fa5d2ad23c34
tree    19d4e173c174b2540ca72772a0cd93863858b56e
parent  21edce0fe247bb2a513b236c504885f8950b002f
...
1
2
3
 
4
5
6
 
 
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
 
 
 
 
 
 
 
22
 
 
...
 
 
 
1
2
 
 
3
4
5
 
 
 
 
 
 
6
 
 
 
 
 
 
 
7
8
9
10
11
12
13
14
15
16
17
0
@@ -1,22 +1,17 @@
0
-require 'rake'
0
-require 'rake/testtask'
0
-require 'rake/rdoctask'
0
+# -*- ruby -*-
0
 
0
-desc 'Default: run unit tests.'
0
-task :default => :test
0
+$:.unshift(File.dirname(__FILE__) + '/lib')
0
+$:.unshift(File.dirname(__FILE__) + '/lib/cached_values')
0
 
0
-desc 'Test the cached_values plugin.'
0
-Rake::TestTask.new(:test) do |t|
0
- t.libs << 'lib'
0
- t.pattern = 'test/**/*_test.rb'
0
- t.verbose = true
0
-end
0
 
0
-desc 'Generate documentation for the cached_values plugin.'
0
-Rake::RDocTask.new(:rdoc) do |rdoc|
0
- rdoc.rdoc_dir = 'rdoc'
0
- rdoc.title = 'HasCachedValue'
0
- rdoc.options << '--line-numbers' << '--inline-source'
0
- rdoc.rdoc_files.include('README')
0
- rdoc.rdoc_files.include('lib/**/*.rb')
0
+require 'rubygems'
0
+require 'hoe'
0
+require "cached_values"
0
+
0
+Hoe.new('CachedValues', CachedValues::VERSION) do |p|
0
+ # p.rubyforge_name = 'CachedValuesx' # if different than lowercase project name
0
+ p.remote_rdoc_dir = '' # Release to root
0
+ p.developer('Jack Danger Canty', 'rubygems_cached_values@6brand.com')
0
 end
0
+
0
+# vim: syntax=Ruby
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 
 
 
 
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
0
@@ -1,97 +1,4 @@
0
-module CachedValues # :nodoc:
0
- def self.included(base)
0
- base.extend(ClassMethods)
0
- end
0
- module ClassMethods
0
- # USAGE:
0
- #
0
- # a very simple case in which cached_values works just like the .count method on a has_many association:
0
- #
0
- # class Company < ActiveRecord::Base
0
- # caches_value :total_employees, :sql => 'select count(*) from employees where company_id = #{id}'
0
- # end
0
- #
0
- # a more sophisticated example:
0
- #
0
- # class User < ActiveRecord::Base
0
- # has_many :trinkets
0
- # has_many :sales, :through => :trinkets
0
- # caches_value :remaining_trinket_sales_allotted, :sql => '... very complicated sql here ...'
0
- # end
0
- #
0
- # user = User.find(:first)
0
- # user.remaining_trinket_sales_allotted # => 70
0
- # Trinket.delete_all # <= any operation that would affect our value
0
- # user.remaining_trinket_sales_allotted # => 70
0
- # user.remaining_trinket_sales_allotted.reload # => 113
0
- #
0
- # You can also calculate the value in Ruby. This can be done by a string to be eval'ed or a Proc. Both are evaluated
0
- # in the context of the record instance.
0
- #
0
- # class User < ActiveRecord::Base
0
- # caches_value :expensive_calculation, :eval => "some_big_expensize_calculation(self.id)"
0
- # caches_value :other_expensive_process, :eval => Proc.new {|record| record.other_expensize_process }
0
- # end
0
- #
0
-
0
- def caches_value(name, options = {})
0
- reflection = create_cached_value_reflection(name, options)
0
-
0
- configure_dependency_for_cached_value(reflection)
0
-
0
- reflection.options[:cache] ||= reflection.name unless false == options[:cache]
0
-
0
- cached_value_accessor_method(reflection, ActiveRecord::CachedValue)
0
- cached_value_callback_methods(reflection)
0
- end
0
-
0
- private
0
-
0
- def configure_dependency_for_cached_value(reflection)
0
-
0
- if !reflection.options[:sql] && !reflection.options[:eval]
0
- raise ArgumentError, "You must specify either the :eval or :sql options for caches_value in #{self.name}"
0
- end
0
-
0
- if reflection.options[:sql] && reflection.options[:eval]
0
- raise ArgumentError, ":eval and :sql are mutually exclusive options. You may specify one or the other for caches_value in #{self.name}"
0
- end
0
- end
0
-
0
- def create_cached_value_reflection(name, options)
0
- options.assert_valid_keys(:sql, :eval, :cache, :clear, :load, :reload)
0
-
0
- reflection = ActiveRecord::Reflection::MacroReflection.new(:cached_value, name, options, self)
0
- write_inheritable_hash :reflections, name => reflection
0
- reflection
0
- end
0
-
0
- def cached_value_accessor_method(reflection, association_proxy_class)
0
- define_method(reflection.name) do |*params|
0
- force_reload = params.first unless params.empty?
0
- association = instance_variable_get("@#{reflection.name}")
0
-
0
- if association.nil? || force_reload
0
- association = association_proxy_class.new(self, reflection)
0
- instance_variable_set("@#{reflection.name}", association)
0
- force_reload ? association.reload : association.load
0
- end
0
- association.target.nil? ? nil : association
0
- end
0
- end
0
-
0
- def cached_value_callback_methods(reflection)
0
- %w{clear reload}.each do |operation|
0
- if events = reflection.options[operation.to_sym]
0
- events = [events] unless events.is_a?(Array)
0
- events.map! { |event| event.to_s }
0
- ActiveRecord::Callbacks::CALLBACKS.each do |callback|
0
- if events.include?(callback)
0
- send(callback, Proc.new {|record| record.send(reflection.name).send(operation)})
0
- end
0
- end
0
- end
0
- end
0
- end
0
- end
0
-end
0
+require 'active_record'
0
+require File.expand_path(File.dirname(__FILE__) + "/cached_values/cached_value")
0
+require File.expand_path(File.dirname(__FILE__) + "/cached_values/cached_values")
0
+ActiveRecord::Base.send :include, CachedValues
...
2
3
4
5
6
7
8
9
10
11
 
12
13
14
...
2
3
4
 
5
6
7
8
9
 
10
11
12
13
0
@@ -2,13 +2,12 @@
0
 # require File.expand_path(File.dirname(__FILE__) + "/test_helper")
0
 
0
 $:.unshift(File.dirname(__FILE__) + '/../lib')
0
-RAILS_ROOT = File.dirname(__FILE__)
0
 
0
 require 'rubygems'
0
 require 'test/unit'
0
 require 'active_record'
0
 require 'active_record/fixtures'
0
-require "#{File.dirname(__FILE__)}/../init"
0
+require "cached_values"
0
 require File.expand_path(File.dirname(__FILE__) + "/leprechaun")
0
 
0
 config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))

Comments

    No one has commented yet.