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
Search Repo:
converted README to Hoe format
Jack Danger Canty (author)
Thu Mar 06 09:12:11 -0800 2008
commit  6a4ee4d18992774d98172e33951534318dc358f2
tree    bcddd95ce702ad732b9e2d77481ac15d2896eeaf
parent  0656cb6c35ffb0d5c0f344c4382f1a488c4907b0
...
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
...
 
 
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
0
@@ -1,62 +1,67 @@
0
-Cached Values
0
-==============
0
+= Cached Values
0
 
0
-* http://github.com/JackDanger/cached_values_gem/
0
+* http://github.com/JackDanger/cached_values/
0
 
0
-A dead-simple way to calculate any value via Ruby or SQL and have it saved in a database field.
0
+A dead-simple way to calculate any value via Ruby or SQL and (optionally) have it saved in a database field.
0
 
0
-Example
0
-=======
0
+== REQUIREMENTS:
0
 
0
-USAGE:
0
+* ObjectProxy
0
 
0
+== INSTALL:
0
+
0
+* as gem: sudo gem install cached_values
0
+* as plugin: ./script/plugin install git://github.com/JackDanger/cached_values.git
0
+
0
+== USAGE:
0
+
0
 You can calculate values with a single line in any ActiveRecord model. To actually save those values you'll need to create
0
 an attribute in your schema. By default the cached_value instance will look for an attribute with the same name as itself.
0
 You can override this by specifying a :cache => :some_attribute option
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
+ class Leprechaun < ActiveRecord::Base
0
+ caches_value :total_gold_coins, :sql => 'select count(*) from gold_coins where leprechaun_id = #{id}'
0
   end
0
+
0
+ Company.find(4).total_employees # => 45
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
+ class Leprechaun < ActiveRecord::Base
0
+ has_many :lucky_charms
0
+ has_many :deceived_children, :through => :lucky_charms
0
+ caches_value :total_children_remaining_to_be_deceived, :sql => '... very complicated sql here ...'
0
   end
0
-
0
-reload() clears the cache and recalculates the value:
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
-The same effect could be had by calling clear() which clears the cache and removes the cache instance from memory
0
+ Leprechaun.find(14).total_children_remaining_to_be_deceived # => 6,692,243,122
0
 
0
+The values can be of any type. The plugin will attempt to cast SQL results to the type corresponding with their database cache
0
+but calculations in Ruby are left alone.
0
+
0
 You can also calculate the value in Ruby using a string to be eval'ed or a Proc. Both are evaluated
0
 in the context of the record instance.
0
 
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
+ class Leprechaun < ActiveRecord::Base
0
+ caches_value :total_gold, :eval => "some_archaic_and_silly_calculation(self.gold_coins)"
0
+ caches_value :total_lucky_charms, :eval => Proc.new {|record| record.calculate_total_lucky_charms }
0
+ end
0
 
0
 The cache is customizable, you can specify which attribute should be used as a cache:
0
   
0
- caches_value :full_formula, :sql => '...' # uses 'full_formula' column if it exists
0
- caches_value :standard_deviation, :sql => '...', :cache => 'std' # uses 'std' column if it exists
0
- caches_value :id, , :sql => '...', :cache => false # does NOT save any cache. This avoids overwriting an attribute
0
+ caches_value :runic_formula, :sql => '...' # uses 'full_formula' column if it exists
0
+ caches_value :standard_deviation_of_gold_over_time, # uses 'std' column if it exists
0
+ :sql => '...', :cache => 'std'
0
+ caches_value :id, :sql => '...', :cache => false # does NOT save any cache. This avoids overwriting an attribute
0
 
0
-Callbacks can be used to call load(), clear(), or reload() at certain times:
0
+ActiveRecord callbacks can be used to call load (make sure the value is in memory), clear (flush the cache and
0
+delete the instance), or reload (flush cache and reload instance) at certain times:
0
   
0
   caches_value :standard_deviation, :sql => '...', :reload => [:after_save, :before_validation]
0
   caches_value :full_formula, :sql => '...', :clear => :after_find
0
 
0
-
0
-Bugs can be filed at http://code.lighthouseapp.com/projects/4502-hoopla-rails-plugins/overview
0
+Bugs can be filed at http://code.lighthouseapp.com/projects/4502-hoopla-rails-plugins/
0
 
0
 Copyright (c) 2007 Jack Danger Canty @ http://6brand.com, released under the MIT license

Comments

    No one has commented yet.