Erb Caching in a Handful #
Dr. Eric Hodel has a brief class for caching compiled ERb templates. The compiled templates aren’t stored on the filesystem—it’s not that kind of cache. It caches the template in an instance variable to prevent recompiling if you’re reusing a template.
Of particular interest to some of you might be his use of method_missing
, which our goopy red hands are continually poking around in the dark for.
def method_missing(action, options = {}) super unless self.respond_to? "run_#{action}" options.each do |attr, value| self.class.send :attr_accessor, attr unless self.respond_to? attr self.send "#{attr}=", value end return self.send("run_#{action}") end
Hmm. You’ve got some self.class.send
and some self.send
. I really think this is an excellent fourteen lines of code for you kids who just got here. Any newbs want to take a stab at explaining?
yerejm
Ooooh, and here I was doing
[instance||class]_eval
.Zsombor
flgr
I think doing
self.class.send
is a bad idea in this case as it won’t allow you to have multiple ERb cache instances that work independently of each other. It’s another case for our good friend, the meta class.yerejm
And with flgr’s comment, I suddenly get the metaclass .
Comments are closed for this entry.