I am really happy to announce Ribs 0.0.2. This version changes loads of things – I realized that I didn’t really like some of the decisions I made for the first version, so this release is totally incompatible with 0.0.1. I haven’t added associations yet, since I wanted to push this out with the new way of defining stuff before doing that.
Ribs is a library for JRuby, that allows you to persist Ruby objects using Hibernate. Some time ago I wrote about ActiveHibernate. I have now decided to implement this myself, and the result is the Ribs project.
The functionality in 0.0.2 is about on par with 0.0.1. You can do a few more things, but not much.
I have made some simplifications which means that you never need to define anything as long as your model follows conventions. That means basically that you can just start working with a class or an instance of a class, if you want too.
So what are the main differences compared with the first version? The first one is how you define mappings from properties to other things. It now looks like this:
class Blog Ribs! :table => :fox_blog do |r| r.id.primary_key! r.title :column => :blog_title r.body :column => :dfsvdfdgdf_TEXT r.something.avoid! end end
These changes will make it easier to add support for complex objects and associations later on.
Ribs has an identity map on by default. It can be turned off on a definition by definition basis by an option to the Ribs! method call.
But the really large difference with this version of Ribs is that it no longer puts anything in the model classes. Yeah, that’s right. All database dependent state is kept elsewhere. This means that you’ll have to define your classes yourself (or use some helpers I’ve defined). If there is no accessor for a property, Ribs will set data on the classes using instance variables instead. The data in a specific instance is not tied in any way to a specific repository or to a specific mapping. I’ve put the layers in to make it easy for a model to have several different mappings for different databases.
So how do you do things with the database if no methods are on the model, and still have it look nice? Like this (assuming Blog has attr_accessors):
blog = R(Blog).create :id => 1, :title => "Foo" blog.body = "Well now, look at this:" R(blog).save blog = R(Blog).all.first R(blog).destroy! R(Blog).get(1)
OK, I admit, there’s three more characters to type for all this stuff. I know, it sucks. No, not really. I’ve done stuff with it for a week now, and it feels quite natural. And there are some other benefits to this approach. The large one is obviously that there is no pollution of the models going on. They are really just simple POROs (and a Struct works quite well for these objects). When you call R, you can supply an optional database parameter, meaning that you can work with the same model – even the same instance – against different databases. There is also some other benefits tied to the fact that all database operations can be parameterized like this. And the base case still feels quite nice.
So, that’s Ribs 0.0.2. I would really like comments on it. The next one should be the big one – with associations of some kind.
More documentation can be found here: http://olabini.com/projects/ribs/doc.
You can download the gem at: http://olabini.com/projects/ribs/downloads/ribs-0.0.2.gem.
The git repository is at: git://github.com/olabini/ribs.git.
You can also install it through gems.
The project is released under the MIT license.
6 Comments, Comment or Ping
Is Ribs inspired by the DDD repository pattern? I like the idea of keeping domain objects unaltered and encapsulating persistence concerns into repository. Looking forward to having an out-of-box solution available simply by typing “R”!
September 17th, 2008
I like this new way of doing things… and I think it has a lot of potential. It reminds me a lot of how jQuery does things (totally different domain, I know – but the similarities are still there) where you can unobtrusively select and manipulate objects.
September 18th, 2008
Great idea… Except it won’t work in Camping since it already got an R-method…
September 18th, 2008
Magnus:
Yeah, totally true. I haven’t actually handled this yet, but I’m planning on making R possible to scope, and there is also a longer method name you can use that doesn’t clash. Not sure if Camping scopes the use of R, but it should since it’s only possible to use it in a very restricted sense.
September 18th, 2008
Both AR and Datamapper have validations built into the model. Are there any plans to have validations added to the Ribs models?
September 20th, 2008
Reply to “Announcing Ribs 0.0.2”