A before_filter equivalent for general Ruby...
I’ve been able to wrap individual methods in other calls via alias_method for a while now, but I’ve always done it by hand; been meaning to write something up to do it generically… Well, I came across an ugly but effective bit of code from Tadayoshi Funaba in Programming Ruby that was easily adapted to do just what I need.
require 'logger'
require 'utility'
class Test
def initialize
@logger = Logger.new(STDOUT)
end
def bar(baz, glarch)
puts [baz, glarch].join('/')
end
def log(*args)
@logger.info(args)
end
before :bar, :log ###Here's the magic line!
end
test = Test.new
test.bar(1, 2)
Outputs:
$ ruby test.rb I, [2008-09-10T09:44:07.371000 #3292] INFO -- : [1, 2] 1/2
…Thanks to that “before :bar, :log”, log() automatically got called before bar() with the same arguments.
Here’s the alteration to Module that does it:
class Module
private
def before(wrapped_method, pre_method)
module_eval <<-EOD
alias_method :__#{wrapped_method.to_i}__, :#{wrapped_method.to_s}
private :__#{wrapped_method.to_i}__
def #{wrapped_method.to_s}(*args, &block)
#{pre_method.to_s}(*args, &block)
__#{wrapped_method.to_i}__(*args, &block)
end
EOD
end
end
Module#after() should work similarly.
Read more...Well, it sounds like Google Chrome could offer a substantial speed increase to AJAX apps. The Javascript VM that compiles down to native instructions is interesting if nothing else is. And hey, if Ben Goodger is on board, it can’t be any worse than Firefox, right?
Update 2008-09-02 13:20:21: No noticeable speed increase on GMail or Reader in Google Chrome over Firefox 3 on XP. Rest of experience is different but decent.
Read more...Monty Python passes the torch...
Just watched my first episode of The Wrong Door - it’s like Flying Circus has been resurrected. (With crappy CG!)
Only those with a U.K. IP address are supposed to be able to get to it, but there’s some kind of fluke that makes it available right now. I’m not gonna count on its release over here; watch while you can.
Read more...Well, that was easy.
I showed Diana the demo rails site I was working on for Jeremy, but she wasn’t much impressed. She used layman’s terms, but in effect said she was sure the modelling and authentication work I’d done was very impressive, but that she couldn’t really appreciate that if all she saw was a plain white screen with some forms on it. Visual design is not my area, so I wasn’t looking forward to the effort required to make something pretty.
Well, one visit to a CSS templates site later, I have something I can drop into my application.html.erb, a default.css I can drop in public/stylesheets, and a much prettier demo. No sweeping revision of my entire code base, no GUI layout editors, and almost no hand-editing. Better yet, all I have to do to make the site skinnable is say
<%= stylesheet_link_tag(session[:user].style || "default") %>
in the default layout, and add a style attribute to the User model. (This last idea from Chad Fowler’s Rails Recipes.) Pretty freakin’ sweet.
Read more...Thunderstorm...
Insane storm tonight - lightning every half second, and the thunder is a constant roar instead of single bursts. Got a couple pictures…
Read more...