RestKit/RestKit — pull request #819 by ecordell (2012-06-17 21:53:33+00:00)
Title: Dynamic findBy and findAllBy methods on NSManagedObject

This pull request modifies NSManagedObject so that any subclass can call methods of the form

`[MyManagedObject findByName:@"Foo"]`

and

`[MyManagedObject findAllByUuid:@"F_b_o_a_o_r"]`

It works for any subclass of NSManagedObject, and returns nil if the property does not exist in the class.

***But Why?***
ActiveRecord is awesome, and NSManagedObject+ActiveRecord and MagicalRecord don't quite capture the magic. And it was something of a programming exercise for me to get comfortable with the objc runtime.

***How it Works***
It's actually pretty cool: any time a selector is sent to a class that the class doesn't recognize, `resolveInstanceMethod:` or `resolveClassMethod:` gets called.

In a subclass, you could override that method to have it do what you want, in this case, respond to a `findBy(Property)` method. But the magic of RestKit's (and MagicalRecord's, etc) CoreData classes is that you get to work with a vanilla NSManagedObject anywhere you want to.

You can't just "override" a method in a category, though. To get around this, I swizzle the methods so that my implementation of resolveClassMethod gets called instead. And I'm using JRSwizzle to do this, so everything is fine with inheritance and whatnot. If the method exists already (or doesn't start with findBy or findAllBy), I swizzle 'em back and pass the selector back to the normal resolveClassMethod:.

***What's left to do***

* Find a way to quiet the "might not respond to selector" warnings.
* Allow different parameter types. Right now I've only tested NSString.
* Allow uppercase properties. Right now it assumes a camel-cased property with a lowercase first letter.
* Allow multiple parameters, i. e. `[MyManagedObject findByCategory: @"Fruit" color:@"Red"];` (I have some of this worked out already)
* Test it. I've tested on the simulator in an actual app, but I can't run the RK test suite right now due to a nokogiri issue (this is slowing my down on the syncing work, too). I'm upgrading to lion within the next two days and I suspect this will fix that problem.
* Pull into standalone categories. It would be nice if people not using RK could have these, too.