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
JackDanger (author)
Thu Mar 06 12:25:38 -0800 2008
commit  454b40da5f99b9806ea1499a141be9704b22d0fb
tree    ffd41d04cc47c54e858c92fa3d7db23c1311247e
parent  cc6c8586b1be9721a8df0e20666f351600c5e05c
name age message
file History.txt Thu Mar 06 08:55:51 -0800 2008 okay, so I don't know how to merge two non-rela... [JackDanger]
file MIT-LICENSE Thu Aug 30 14:39:26 -0700 2007 adding has_cached_value plugin. Damn this is a... [studioda]
file Manifest.txt Thu Mar 06 12:25:38 -0800 2008 Revert "Revert "updating Manifest&quo... [JackDanger]
file README.txt Thu Mar 06 09:12:11 -0800 2008 converted README to Hoe format [JackDanger]
file Rakefile Thu Mar 06 08:55:51 -0800 2008 okay, so I don't know how to merge two non-rela... [JackDanger]
file init.rb Fri Aug 31 08:58:19 -0700 2007 refactored the pieces apart [studioda]
file install.rb Fri Aug 31 10:38:29 -0700 2007 printing README on install [studioda]
directory lib/ Thu Mar 06 09:38:54 -0800 2008 okay, so 'rake test' still breaks everything. ... [JackDanger]
directory tasks/ Fri Aug 31 08:14:52 -0700 2007 finished most of the renaming [studioda]
directory test/ Thu Mar 06 09:38:54 -0800 2008 okay, so 'rake test' still breaks everything. ... [JackDanger]
file uninstall.rb Thu Aug 30 14:39:26 -0700 2007 adding has_cached_value plugin. Damn this is a... [studioda]
README.txt
= Cached Values

* http://github.com/JackDanger/cached_values/

A dead-simple way to calculate any value via Ruby or SQL and (optionally) have it saved in a database field.

== REQUIREMENTS:

* ObjectProxy

== INSTALL:

* as gem: sudo gem install cached_values
* as plugin: ./script/plugin install git://github.com/JackDanger/cached_values.git

== USAGE:

You can calculate values with a single line in any ActiveRecord model.  To actually save those values you'll need to 
create
an attribute in your schema.  By default the cached_value instance will look for an attribute with the same name as 
itself.
You can override this by specifying a :cache => :some_attribute option

A very simple case in which cached_values works just like the .count method on a has_many association:

  class Leprechaun < ActiveRecord::Base
   caches_value :total_gold_coins, :sql => 'select count(*) from gold_coins where leprechaun_id = #{id}'
  end
  
  Company.find(4).total_employees # => 45

A more sophisticated example:

  class Leprechaun < ActiveRecord::Base
   has_many :lucky_charms
   has_many :deceived_children, :through => :lucky_charms
   caches_value :total_children_remaining_to_be_deceived, :sql => '... very complicated sql here ...'
  end
  
  Leprechaun.find(14).total_children_remaining_to_be_deceived # => 6,692,243,122

The values can be of any type.  The plugin will attempt to cast SQL results to the type corresponding with their 
database cache
but calculations in Ruby are left alone.

You can also calculate the value in Ruby using a string to be eval'ed or a Proc.  Both are evaluated
in the context of the record instance.

  class Leprechaun < ActiveRecord::Base
    caches_value :total_gold, :eval => "some_archaic_and_silly_calculation(self.gold_coins)"
    caches_value :total_lucky_charms, :eval => Proc.new {|record| record.calculate_total_lucky_charms }
  end

The cache is customizable, you can specify which attribute should be used as a cache:
  
  caches_value :runic_formula, :sql => '...'          # uses 'full_formula' column if it exists
  caches_value :standard_deviation_of_gold_over_time, # uses 'std' column if it exists
               :sql => '...', :cache => 'std'
  caches_value :id, :sql => '...', :cache => false    # does NOT save any cache.  This avoids overwriting an attribute

ActiveRecord callbacks can be used to call load (make sure the value is in memory), clear (flush the cache and
delete the instance), or reload (flush cache and reload instance) at certain times:
  
  caches_value :standard_deviation, :sql => '...', :reload => [:after_save, :before_validation]
  caches_value :full_formula, :sql => '...', :clear => :after_find

Bugs can be filed at http://code.lighthouseapp.com/projects/4502-hoopla-rails-plugins/

Copyright (c) 2007 Jack Danger Canty @ http://6brand.com, released under the MIT license