Why I am a DataMapper convert (it makes BDD happy)
T here’s lots of buzz around DataMapper right now, especially among the #merb crowd. I haven’t had any major issues with ActiveRecord, so I haven’t needed to look at a new ORM…but I like to play with new things, so last night I really gave the DM a try.
I’m a complete convert, if for no other reason that the awesome way automigrations play along with BDD.
First, tell your spec_helper to use automigrations:
DataMapper::Base.auto_migrate!
This will cause your database tables to be dropped and re-created so use with caution.
Next, let’s spec some behavior:
it "should have a username field" do @person.username = "jjames" @person.username.should == "jjames" end
And see what autotest says:
....F.....
1)
NoMethodError in 'Person should have a username field'
undefined method `username=' for #<person:0x13f720c>
./spec/models/person_spec.rb:38:
Finished in 0.049775 seconds
10 examples, 1 failure
Cool, that’s what we expected. Now, with ActiveRecord, to get to green, we’d need to create a migration and run it. With DataMapper, we stay in our model:
class Person < DataMapper::Base property :username, :string end
And autotest says…
..........
Finished in 0.040584 seconds
10 examples, 0 failures
Now, is this perfect? Not yet. DM has a good amount of work still. There’s some smart folks working on it, and I bet it will be pretty sweet in a few more weeks.
But, man…that’s just awesome.