0
-* http://github.com/JackDanger/cached_values
_gem/
0
+* http://github.com/JackDanger/cached_values
/
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
+* as gem: sudo gem install cached_values
0
+* as plugin: ./script/plugin install git://github.com/JackDanger/cached_values.git
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
A very simple case in which cached_values works just like the .count method on a has_many association:
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
+ Company.find(4).total_employees # => 45
0
A more sophisticated example:
0
- class User < ActiveRecord::Base
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
-reload() clears the cache and recalculates the value:
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
-
The same effect could be had by calling clear() which clears the cache and removes the cache instance from memory0
+
Leprechaun.find(14).total_children_remaining_to_be_deceived # => 6,692,243,1220
+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
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
- 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
The cache is customizable, you can specify which attribute should be used as a cache:
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
-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
caches_value :standard_deviation, :sql => '...', :reload => [:after_save, :before_validation]
0
caches_value :full_formula, :sql => '...', :clear => :after_find
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
Copyright (c) 2007 Jack Danger Canty @ http://6brand.com, released under the MIT license
Comments
No one has commented yet.