In Ruby with lafcadio, O / R mappingfrancis hwanghttp: //lafcadio.rubyforge.org/sera@fhwang.net translation finishing: http://www.ruby-cn.org Reprint Please keep it. About Lafcadio I started writing Lafcadio mainly for yourself, in August 2003, I built this project in RubyForge, and its main role is to concentrate on the Ruby code, not how to operate mysql. This also makes the unit test easier. There are already some websites to use it, including rhizome.org. User Domain Class For example, we can write a USER's Domain class. 1. XML and Ruby class definition % Lafcadio_schema -c config.dat User.rbcreate table users (objId int not null auto_increment, primary key (objId), firstName varchar (255) not null, lastName varchar (255) not null, email varchar (255) not null, password VARCHAR (255) Not Null, Birthday Date Not Null; 3. Create and Commit An InstancetWentyearsago = Date.today - (365 * 20) John = User.new ({'firstname' => 'john', 'lastname' = > 'Doe', 'email' => 'john.doe@email.com', 'password' => 'my_password', 'birthday' => twentyyearsago}) PUTS JOHN.EMAIL => "john.doe@email. com "puts john.objId => niljohn.commitputs john.objId => 14. select and edit objectStore = ObjectStore.getObjectStorejohn = objectStore.getUser (1) john.birthday = john.birthday - (365 * 10) john.email = 'john.doe@hotmail.com'john.commit # or objectStore.commit (john) 5. delete record objectStore = ObjectStore.getObjectStorejohn = objectStore.getUser 1john.delete = trueobjectStore.commit john6. Add a new method class User 1. Define Message Domain allMessages = objectStore.getAll (Message) messagesByJohn = objectStore.getMessages (john, 'author') messagesToJohn = objectStore.getMessages (john, 'recipient') hotmailCondition = Query :: Like.new ( 'email', 'hotmail.com' , User) puts hotmailCondition.toSql => "email like '% hotmail.com%'" usersWithHotmailAddresses = objectStore.getSubset (hotmailCondition) test-driven database design Once you are hidden behind the ObjectStore, you can put MockObjectStore as a database Test it. class TestSaveMessage