Ramping up Rails with MOGenerator

I often find the need for a rails backed iPhone app. I do some modeling of my domain objects in the iPhone and get the basic lay of the land.  Then I go off and create the rails world, determine the resource structure and add a JSON interface to the rails app.  Then I go back to the iPhone and add the HTTP Requests and JSON loading. As the app develops, this definitely diverges, but for the initial spike, I keep thinking “Can’t I just generate my rails model off Core Data?”.

Enter MOGenerator, which I think everyone should be using on their CoreData models. It is a project that links a template language (MiscMerge, an apparently abandoned package) with the NSEntityDescription objects to build great wrappers for CoreData. This separates your code from the machine generated code and keeps you from dreading changes to your data model. Turns out this template language can generate any text. My first thought was to use this to generate rails code, and soon there after I realized how much work that would be. I don’t want Core Data to be the authority of my rails data model though, I just want to cut out some tedious work in bootstrapping the rails app.

So I modified MOGenerator to… generate ./script/generate commands.  So yes, generating a shell command to generate rails code.   It doesn’t attempt to be a perfect (or complete) mapping of CoreData to Rails, just to save some time.

The modifications to MOGenerator are very simple. MOGenerator is dynamic in what it can generate in it’s template files, BUT it hardcodes filenames in the program.  My fork pushes the filename selection out into the template. I found another github project that forked just to change the hardcoding, so I’m hoping that rentzsch incorporates the patch.

Moving forward I see two main things

  • Modifying templates to use [EntityName].[hm] and [EntityName]+custom.[hm] rather than inheritance. It’s just cleaner in my book to seperate with Categories rather than inheritance.
  • Pushing some busy-work JSON loading into [EntityName]+loadJSON.[hm]. This would define the JSON keys for each attribute into userInfo if it’s source was JSON.

And if I were to dream, I see pushing the authority for my CoreData model into rails and auto-generating that from my rails model. That way if I wanted an object or attribute to show up in my app, I could add a key and it would get added to the whole stack, and add versioning support…..   But reality quickly squashes that dream !


Leave a Reply