As I was writing
doodlekit, I found a few places where I need a conditional column. Something that didn't necessarily need to be there all the time, and didn't feel right to create a new table column. This especially came into play as I started working on a form builder where the entire record is dynamic. So I created a plugin called acts_as_extensible.
Basically you add a field called xml to your table, and you can add columns to your Model on the fly. I got the idea from my former boss. These columns can either be defined ahead of time as arguments to the acts_as_extensible call, or dynamically at runtime.
class SpaceShip < ActiveRecord::Base acts_as_extensible :food_stamps, :tube_sock This will automatically create accessors for each argument, so you could say.
space_ship = SpaceShip.new space_ship.tube_sock = 'Stink Mucus' puts space_ship.tube_sock Alternatively you could say
class SpaceShip < ActiveRecord::Base acts_as_extensible And
space_ship = SpaceShip.new space_ship.set_element(:tube_sock, "I'm a gigantic brain") puts space_ship.element(:tube_sock) I only use this in very exceptional cases, and by no means would recommend using it if your just too lazy to create a new column. Right now it only supports one layer, but could possibly support more complex data structures.
To install this plugin, just
download it, and extract it to your plugins directory. To use it, add a field called "xml" to your table, and see the code snippet above.
If there's enough interest, which I doubt there will be, I'll do a ruby forge project.
Note: Content for examples was extracted from the episode of Futurma I'm watching right now.
Post a Comment