Get Rid of ActiveRecord Situps

by chromatic

Rails fans are understandably proud of the magic metaprogramming facilities of Ruby, the database introspection capabilities of ActiveRecord, and the fact that the most basic model class is only two lines long (at least in every tutorial I've seen).

I say that's two lines too many.

Here's how to have zero-line model modules in Perl -- as many as you want. (If you have a complete CRUD application, you can use the same idea to generate RESTful controllers, too.)


7 Comments

Jeremy Kemper
2006-11-17 17:46:14
Heya chromatic! Fortunately, Bryan Evans is on the case with the DrySQL plugin for Active Record.


Active Record doesn't need to use eval at all since Ruby has such niceties as define_method, const_missing, and method_missing. However, define_method captures scope so when classes are reloaded in development mode you have a memory obesity issue that eval doesn't suffer.


You can do
class Prufrock < ActiveRecord::Base; end
or
const_set some_model_name, Class.new(ActiveRecord::Base)
or even

module Models
def self.const_missing(name)
const_set name, Class.new(ActiveRecord::Base)
end
end

then Models::Prufrock or Models::WhateverElse


Cheers!

Danno
2006-11-18 12:30:34
DrySQL to the rescue: http://drysql.rubyforge.org/
perl defector
2006-11-18 13:39:50
Man, years of submerging in perl still leave me in a desert when looking at your code. Another reason for me to like ruby and ror. I like simplicity and clearness.
Greg
2006-11-18 15:42:07
chromatic,


were you trying to show this to be more convenient than Ruby/RoR?


Man, I don't know. As someone who does both perl and ruby, this may not be the best example! :)

Timothy Appnel
2006-12-02 08:55:01
Interesting and perhaps a bit far out there with seeing GenModel.Have you reviewed Data::ObjectDriver?
mda
2006-12-05 08:06:12
Why is using eval so bad in this case? What you get is code (classes) generated at run time. Isn't whether that is done via an eval or meta model poking just a (language specific) implimentation detail?
curious
2007-02-13 12:02:11
i really enjoyed this article and am hoping you post the follow-up about GenModel soon ...