#100 invalid
Carl

add generate_properties

Reported by Carl | December 17th, 2007 @ 12:07 AM

This is some code I wrote tonight to automagically generate properties at runtime for a given model. It mimics ActiveRecord's behavior.

The reason for this is because we have a "legacy" Rails app, and we're building a Merb app that talks to the same database. We want to keep our migrations in Rails and sync'ing our DM models with our Rails migration was not very DRY.

No code is generated/written to the model file. Instead, the database schema is loaded when the class is loaded in memory and properties are added dynamically. It is only done once, not on every instantiation.

To use the code:

class Account < DataMapper::Base

generate_properties

end

My code only works with MySQL and my tests are Test::Unit (sorry!)

I think it'd be a great addition to the core, but it will need some work to make it compatible with other DBs. I'm not familiar with DM's architecture; I did my best to make it as unobtrusive as possible, but you'll surely want to rewrite some parts if you want to include it in DM.

Thanks for DM!!!

Comments and changes to this ticket

  • Carl

    Carl December 17th, 2007 @ 10:28 AM

    I just updated the code to allow manual property assignment and to accept "exceptions". this is now valid:

    class Account < DataMapper::Base

    property :name, :string, :nullable=>false, :default=>'John Doe'

    generate_properties :except=>[:date_of_birth, :city]

    end

    All the properties will be automatically generated EXCEPT :name since it's been manually added. :date_of_birth and :city won't be accessible at all as they are specified in the :except clause.

    This allows more flexibility when one wants to have some custom properties and some automatic ones.

  • Sam Smoot

    Sam Smoot December 17th, 2007 @ 06:38 PM

    • State changed from “new” to “open”

    Awesome. I'll be back on irc#datamapper tomorrow. Ping me about this if you have the time?

  • Carl

    Carl December 18th, 2007 @ 12:15 PM

    what's your nickname on irc?

  • Sam Smoot
  • Sam Smoot

    Sam Smoot December 27th, 2007 @ 10:12 AM

    • State changed from “open” to “invalid”

    k, so just in case someone wants to come back later and spec this out for all databases and resubmit, I'm marking this as invalid for now.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Attachments

Pages