OR mapping with Lafcadio in Ruby

xiaoxiao2021-03-06  57

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 class user

% 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 "john doe" Domain object's association below We will see how we will see Use LafCadio to establish a relationship between two Domain.

1. Define Message Domain class Message 'Hey', 'Body' => MessageBody, 'Author' => John, 'Recipient' => Jane, 'Datest' => Date.Today} Puts Message.author.FirstName => "john "The default properties of the message.commit override table have a lot of tables in lafcadio with default properties, which we can easily overwrite these properties. selecting a set of class ExampleClass 10 choose if you want to record a lot, Lafcadio provides some different method.

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

转载请注明原文地址:https://www.9cbs.com/read-93986.html

New Post(0)